Priority Queue in C++ Standard Template Library (STL)

Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non decreasing order(hence we can see that each element of the queue has a priority{fixed order}).
The functions associated with priority queue are:
empty() – Returns whether the queue is empty
size() – Returns the size of the queue
top() – Returns a reference to the top most element of the queue
push(g) – Adds the element ‘g’ at the end of the queue
pop() – Deletes the first element of the queue

#include <iostream>
#include <queue>

using namespace std;

void showpq(priority_queue <int> gq)
    priority_queue <int> g = gq;
    while (!g.empty())
        cout << '\t' <<;
    cout << '\n';

int main ()
    priority_queue <int> gquiz;

    cout << "The priority queue gquiz is : ";

    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ : " <<;

    cout << "\ngquiz.pop() : ";

    return 0;

The output of the above programs is :

The priority queue gquiz is :     30    20    10    5    1

gquiz.size() : 5 : 30
gquiz.pop() :     20    10    5    1

How to implement Min Heap using priority queue?
Prim’s algorithm using priority_queue in STL
Dijkstra’s Shortest Path Algorithm using priority_queue of STL
Greedy Algorithms | Set 3 (Huffman Coding)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Recommended Posts:

1.8 Average Difficulty : 1.8/5.0
Based on 26 vote(s)