priority_queue::push() and priority_queue::pop() in C++ STL
Last Updated :
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.
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.
Time complexity: O(log n)
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
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue< int > pqueue;
pqueue.push(3);
pqueue.push(5);
pqueue.push(1);
pqueue.push(2);
while (!pqueue.empty()) {
cout << ' ' << pqueue.top();
pqueue.pop();
}
}
|
Output:
5 3 2 1
priority_queue::pop()
pop() function is used to remove the top element of the priority queue.
Time complexity: O(log n)
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
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue< int > pqueue;
pqueue.push(0);
pqueue.push(1);
pqueue.push(2);
pqueue.pop();
pqueue.pop();
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
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int c = 0;
priority_queue< int > pqueue;
pqueue.push(5);
pqueue.push(13);
pqueue.push(0);
pqueue.push(9);
pqueue.push(4);
while (!pqueue.empty()) {
pqueue.pop();
c++;
}
cout << c;
}
|
Output:
5
Share your thoughts in the comments
Please Login to comment...