class Event
1: //priority_queue05.cpp
2: //Based on an example of Herbert Schildt.
4: #include <iostream>
5: #include <queue>
6: #include <string>
7: using namespace std;
9: //A simple "event organizer" class
10: class Event
11: {
12: public:
14: Event()
15: {
16: name = "";
17: priority = 0;
18: }
20: Event(string name, int priority)
21: {
22: this->name = name;
23: this->priority = priority;
24: }
26: string getEventName() const { return name; }
27: int getEventPriority() const { return priority; }
28:
29: private:
30: int priority;
31: string name;
32: };
34: //Overload operator< to determine priority:
35: bool operator<(const Event &e1, const Event &e2)
36: {
37: return e1.getEventPriority() < e2.getEventPriority();
38: }
41: int main()
42: {
43: cout << "\nThis program illustrates a simple priority queue "
44: "of class objects,\nin which the priority has been "
45: "defined by overloading \"operator<\".";
46: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
48: priority_queue<Event> pq;
49: pq.push(Event("Phone rings", 3));
50: pq.push(Event("Fire!", 10));
51: pq.push(Event("Mail arrives", 2));
52: pq.push(Event("Knock is heard on door", 4));
54: //Display the list of events, with their priorities:
55: cout << "\nPriorities:\n\n";
56: while(!pq.empty())
57: {
58: cout << pq.top().getEventName() << "\n";
59: pq.pop();
60: }
61: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
62: }