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


Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is the greatest of all elements in the queue.

priority_queue::push()

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
         myqueue.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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

5 3 2 1
priority_queue::pop()

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.