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