Related Articles
Priority Queue in C++ Standard Template Library (STL)
• Difficulty Level : Easy
• Last Updated : 17 Feb, 2021

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 increasing order (hence we can see that each element of the queue has a priority {fixed order}).

## CPP

 `// Note that by default C++ creates a max-heap``// for priority queue``#include ``#include `` ` `using` `namespace` `std;`` ` `void` `showpq(priority_queue<``int``> gq)``{``    ``priority_queue<``int``> g = gq;``    ``while` `(!g.empty()) {``        ``cout << ``'\t'` `<< g.top();``        ``g.pop();``    ``}``    ``cout << ``'\n'``;``}`` ` `int` `main()``{``    ``priority_queue<``int``> gquiz;``    ``gquiz.push(10);``    ``gquiz.push(30);``    ``gquiz.push(20);``    ``gquiz.push(5);``    ``gquiz.push(1);`` ` `    ``cout << ``"The priority queue gquiz is : "``;``    ``showpq(gquiz);`` ` `    ``cout << ``"\ngquiz.size() : "` `<< gquiz.size();``    ``cout << ``"\ngquiz.top() : "` `<< gquiz.top();`` ` `    ``cout << ``"\ngquiz.pop() : "``;``    ``gquiz.pop();``    ``showpq(gquiz);`` ` `    ``return` `0;``}`

Output:

```The priority queue gquiz is :     30    20    10    5    1

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

How to create a min heap for priority queue?
C++ provides below syntax for the same.

// Syntax to create a min heap for priority queue
priority_queue <int, vector<int>, greater<int>> g = gq;

## CPP

 `// C++ program to demonstrate min heap``#include ``#include `` ` `using` `namespace` `std;`` ` `void` `showpq(``    ``priority_queue<``int``, vector<``int``>, greater<``int``> > gq)``{``    ``priority_queue<``int``, vector<``int``>, ``                                ``greater<``int``> > g = gq;``    ``while` `(!g.empty()) {``        ``cout << ``'\t'` `<< g.top();``        ``g.pop();``    ``}``    ``cout << ``'\n'``;``}`` ` `int` `main()``{``    ``priority_queue<``int``, vector<``int``>,  ``                       ``greater<``int``> > gquiz;``    ``gquiz.push(10);``    ``gquiz.push(30);``    ``gquiz.push(20);``    ``gquiz.push(5);``    ``gquiz.push(1);`` ` `    ``cout << ``"The priority queue gquiz is : "``;``    ``showpq(gquiz);`` ` `    ``cout << ``"\ngquiz.size() : "` `<< gquiz.size();``    ``cout << ``"\ngquiz.top() : "` `<< gquiz.top();`` ` `    ``cout << ``"\ngquiz.pop() : "``;``    ``gquiz.pop();``    ``showpq(gquiz);`` ` `    ``return` `0;``}`

Output:

```The priority queue gquiz is :     1    5    10    20    30

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

Note : The above syntax may be difficult to remember, so in case of numeric values, we can multiply the values with -1 and use max heap to get the effect of min heap.
Methods of priority queue are:

Recent articles on priority queue in STL

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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up