priority_queue::push() and priority_queue::pop() in C++ STL

14 Jun, 2022
Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the smallest element at the top by simply passing an extra parameter while creating the priority queue.
push() function is used to insert an element in the priority queue. The element is added to the priority queue container and the size of the queue is increased by 1. Firstly, the element is added at the back and at the same time the elements of the priority queue reorder themselves according to priority. 
Syntax :
pqueuename.push(value)
Parameters :
The value of the element to be inserted is passed as the parameter.
Result :
Adds an element of value same as that of 
the parameter passed in the priority queue.
Examples:
Input :  pqueue
         pqueue.push(6);
Output : 6

Input :  pqueue = 5, 2, 1
         pqueue.push(3);
Output : 5, 3, 2, 1
Errors and Exceptions 1. Shows error if the value passed doesn’t match the priority queue type. 2. Shows no exception throw guarantee if the parameter doesn’t throw any exception. 
CPP
// CPP program to illustrate// Implementation of push() function#include <iostream>#include <queue>using namespace std; int main(){    // Empty Queue    priority_queue<int> pqueue;    pqueue.push(3);    pqueue.push(5);    pqueue.push(1);    pqueue.push(2);    // Priority queue becomes 5, 3, 2, 1     // Printing content of queue    while (!pqueue.empty()) {        cout << ' ' << pqueue.top();        pqueue.pop();    }}
Output:
5 3 2 1
pop() function is used to remove the top element of the priority queue. 
Syntax :
pqueuename.pop()
Parameters :
No parameters are passed.
Result :
The top element of the priority
queue is removed.
Examples:
Input :  pqueue = 3, 2, 1
         myqueue.pop();
Output : 2, 1

Input :  pqueue = 5, 3, 2, 1
         pqueue.pop();
Output : 3, 2, 1
Errors and Exceptions 1. Shows error if a parameter is passed. 2. Shows no exception throw guarantee if the parameter doesn’t throw any exception. 
CPP
// CPP program to illustrate// Implementation of pop() function#include <iostream>#include <queue>using namespace std; int main(){    // Empty Priority Queue    priority_queue<int> pqueue;    pqueue.push(0);    pqueue.push(1);    pqueue.push(2);    // queue becomes 2, 1, 0     pqueue.pop();    pqueue.pop();    // queue becomes 0     // Printing content of priority queue    while (!pqueue.empty()) {        cout << ' ' << pqueue.top();        pqueue.pop();    }}
Output:
0
Application : push() and pop() Given a number of integers, add them to the priority queue and find the size of the priority queue without using size function.
Input : 5, 13, 0, 9, 4
Output: 5
Algorithm 1. Push the given elements to the priority queue container one by one. 2. Keep popping the elements of priority queue until it becomes empty, and increment the counter variable. 3. Print the counter variable. 
CPP
// CPP program to illustrate// Application of push() and pop() function#include <iostream>#include <queue>using namespace std; int main(){    int c = 0;    // Empty Priority Queue    priority_queue<int> pqueue;    pqueue.push(5);    pqueue.push(13);    pqueue.push(0);    pqueue.push(9);    pqueue.push(4);    // Priority queue becomes 13, 9, 5, 4, 0     // Counting number of elements in queue    while (!pqueue.empty()) {        pqueue.pop();        c++;    }    cout << c;}
Output:
5
yasharth234
divyanshmishra101010
CPP-Library
cpp-priority-queue
cpp-queue
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
std::find in C++
List in C++ Standard Template Library (STL)
Inline Functions in C++