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

19 Feb, 2022
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 nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). Following is an example to demonstrate the priority queue and its various methods. 
CPP
// CPP Program to demonstrate Priority Queue#include <iostream>#include <queue>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';} // Driver Codeint 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;}
The priority queue gquiz is :     30    20    10    5    1

gquiz.size() : 5
gquiz.top() : 30
gquiz.pop() :     20    10    5    1
Note: By default, C++ creates a max-heap for priority queue.
How to create a min-heap for the priority queue? C++ provides the below syntax for the same.  
Syntax:
priority_queue <int, vector<int>, greater<int>> g = gq;  
CPP
// C++ program to demonstrate min heap for priority queue#include <iostream>#include <queue>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';} // Driver Codeint 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;}
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: 
Method
Definition
Must Read: Recent articles on priority queue in STL 
YouTubeGeeksforGeeks500K subscribersC++ Programming Language Tutorial | Priority Queue in C++ STL | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:55•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=vc7i0bBuQEM" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.  
cr7_bullet
harshitagrawal920
manasdtrivedi
2104hiteshdhiman
anshikajain26
aqquilin141
cpp-containers-library
cpp-priority-queue
priority-queue
STL
C++
STL
priority-queue
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++
Socket Programming in C/C++
Virtual Function in C++
Templates in C++ with Examples
rand() and srand() in C/C++
vector erase() and clear() in C++
unordered_map in C++ STL