#include #include using namespace std; class myC { public: myC() : key(0), time(0), s("") {} myC(int ky, int t, string str) : key(ky), time(t), s(str) {} //myC(const myC& c); // Copy constructor not req //void operator=(const myC& r); // op= not required int getKey() {return key;} void setKey(int k) {key = k;} string getString() { return s;} friend bool operator<(const myC& c1, const myC& c2); private: int key; int time; string s; }; bool operator<(const myC& c1, const myC& c2) { if (c1.key == c2.key ) { return c1.time > c2.time; } return c1.key > c2.key; } // note the STL priority queue is not stable, therefore we need to add a // secondary sort order (based on time inserted) to create a total order typedef priority_queue myPQueue; int main () { string name[] = {"Tom", "Joe", "Bob", "Ben"}; myPQueue q; myC c; int i; srand(time(0)); for (i = 1; i <= 4; i++) { c = myC(rand()%10, i, name[i-1]); q.push(c); } i = 1; while (!q.empty()) { c = q.top(); cout << "Element " << i++ << ": key = " << c.getKey() << ", s = " << c.getString() << "." << endl; q.pop(); } return 0; }