![]() |
VOOZH | about |
A Priority Queue adds and removes elements according to priority.
Elements removed from priority queue in order: 40 30 20 10
Priority queue is defined as std::priority_queue inside <queue> header file.
priority_queue<T, c, comp> pq;
where,
Declaration of a priority queue means creating it using the STL choosing whether it's a max-heap or min-heap.
priority_queue<int>pq;
priority_queue<int,vector<int>,greater<int>>pq;
Initialization means adding elements to it so it can manage them based on their priority.
pq.push(10);
pq.push(5);
pq.push(20);
Here are the basic operations that can be performed on a priority queue:
Elements can be inserted in the priority queue using push() method. After insertion, priority queue reorganize itself in such a way that the highest priority element is always at the top.
Only the top element of the priority queue can be accessed using top() method. It is the element with the highest priority in priority queue.
9
In priority queue, deletion can only be done from the top of the priority queue using pop()method. It means that we can only remove the element with highest priority in one move. After deletion, the priority queue rearranges itself such that the next greatest priority element becomes the top element.
8
Explanation: In this example, we deleted the top element using pop() method, which is 9. This shifts the next greatest element 8 to the top.
This checks whether the priority queue is empty. It returns true if the priority queue has no elements; otherwise, it returns false.
Priority queue is empty. Priority queue is not empty. Top element 50
The size() function in a priority queue returns the number of elements currently in the priority queue.
Size of priority queue: 2 Size after one pop: 1
In priority queue, we can only access top element of the priority queue, so we cannot directly traverse it. However, we can create a copy of the priority queue, access and delete the top element and repeat this process until the copied priority queue is empty. In this way, we can effectively traverse all the elements of the priority queue.
9 8 6
All the above operations are demonstrated on a priority queue implementing max heap. This can be changed by using a custom comparator in which you define the priority parameter and how to compare them.
If you only want to assign highest priority to the smallest element (min-heap), then inbuilt greater<type> functional object can be used.
6 8 9
Explanation: In the above program, even though vector is default container, we still have to pass it because the comparator is third template argument. So, we have to pass all the arguments before it.
Feature | Queue | Priority Queue |
|---|---|---|
Order of Removal | FIFO (First-In, First-Out) | Based on priority |
Element Priority | All elements treated equally. | Each element has a priority. |
Use case | Task scheduling, print queue | CPU scheduling, Dijkstra's Algorithm |
Default Behavior | Inserts at rear, removes front | Inserts anywhere, removes highest priority |
STL Class in C++ | queue | priority_queue |
Following is the list of all member functions of std::priority_queue class in C++:
Function | Definition |
|---|---|
| empty() | Returns whether the priority queue is empty. |
| size() | Returns the size of the priority queue. |
| top() | Returns top element of the priority queue |
| push() | Adds the element into the priority queue. |
| pop() | Deletes the top element of the priority queue. |
| swap() | Used to swap the contents of two priority queues provided the queues must be of the same type, although sizes may differ. |
| emplace() | Used to insert a new element into the priority queue container. |