Created
February 23, 2019 22:44
-
-
Save sriharshaj/a1713917eb3997e2b24c5c2253b87d03 to your computer and use it in GitHub Desktop.
Priority Queue for structs in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
#include <queue> | |
using namespace std; | |
struct Student{ | |
int age; | |
bool operator<(const Student& s) const{ | |
return age < s.age; | |
} | |
}; | |
class myComparator { | |
public: | |
bool operator() (const Student& s1, const Student& s2){ | |
return s1.age < s2.age; | |
} | |
}; | |
int main() { | |
priority_queue<Student, vector<Student>, myComparator> student_heap; | |
int n= 5; | |
Student* s = new Student [n]; | |
for (int i =0; i< n;i++){ | |
cin>>s[i].age; | |
student_heap.push(s[i]); | |
} | |
for (int i = 0; i<n; i++){ | |
cout<<student_heap.top().age<<endl; | |
student_heap.pop(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment