Qualifying an Element of Priority Queue

The Standard Template Library defines a specialized form of the queue<> container called priority_queue<>. A priority_queue is a queue whose elements are internally sorted according to their priority. Thus, an element with the highest priority is located at the top. To qualify as an element of priority_queue<>, an object has to define the < operator. For example:

 #include  // definition of less<> #include   // definition of priority_queue<>#include using namespace std;struct Task{  int priority;  friend bool operator < (const Task& t1, const Task& t2);  Task(int p=0) : priority(p) {}  };bool operator < (const Task& t1, const Task& t2) { 	return t1.priority < t2.priority;}int main(){  priority_queue scheduler;  scheduler.push(Task(3));  scheduler.push(Task(5));  scheduler.push(Task(1));  cout<< scheduler.top().priority <
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: