Priority Queue in C++
A?priority queue in c++ is a type of container adapter, which processes only the highest priority element, i.e. the first element will be the maximum of all elements in the queue, and elements are in decreasing order.
Difference between a queue and priority queue:
priority_queue<int> p1;
p1.push(35);????????????? // inserting element in a queue
p1.push(40);
p1.push(95);
while (!p1.empty())
{
cout << ' ' << p1.top();? //printing elements of queue
p1.pop();
}
the output will be : 95 40 35
Delete Elements from the priority queue
priority_queue<int> p1;
p1.push(35);???
p1.push(40);
p1.push(95);
p1.push(20);
// queue : 95 40 35 20
p1.pop();??????????? // queue :? 40 35 20
p1.pop();?????????? // queue :? 35? 20
?while (!p1.empty())
{
????????cout << ' ' << p1.top();???????????????
????????p1.pop();
领英推荐
????}
???Methods of Priority Queue:
Following are the methods of Priority Queue :
syntax?:?p1.empty()???????????// p1 is priority_queue object
syntax :?p2.size()??????????????// p2 is priority_queue object
syntax :?p3.push(value)???????//value to be inserted
syntax :?p3.pop()?????// p3 is priority_queue object
syntax :?p3.top()??
syntax :?p3.swap(p1)???
syntax :?p3.emplace(value)??
The importance of the priority queue is to process the elements based on priority.
You can also implement the priory queue from scratch using the following data structures:?
Applications of Priority Queue:?