Mega Code Archive

 
Categories / C++ / Data Structure
 

Storing class objects in a priority_queue

#include <iostream> #include <queue> #include <string> using namespace std; class Thread {   int priority;   string name; public:   Thread() {       name = "";       priority = 0;    }   Thread(string n, int p) {       name = n;       priority = p;    }   string getname() const {       return name;    }   int getpriority() const {       return priority;    } }; // Determine priority. bool operator<(const Thread &a, const Thread &b) {   return a.getpriority() < b.getpriority(); } int main() {   priority_queue<Thread> q;      q.push(Thread("F", 10));   q.push(Thread("M", 2));   // show priority   cout << "Priorities: ";    while(!q.empty()) {     cout << q.top().getname() << endl;     cout << "            ";     q.pop();   }   return 0; }