C++ prioriy_queue push function is used to insert an element into a priority queue. The element is added into the priority queue container then the size of the priority queue is increased by 1. Firstly, the element is added at the back and at the same time elements of the priority queue reorder themselves according to priority.
Syntax
Consider priorityqueue 'pq' as a priorityqueue object.
Example
pq.push (value);
Parameter
Value : It is used to add value into a priority queue.
Return value
Example 1
Example
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<char> mp;
mp.push('c');
mp.push('d');
mp.push('a');
mp.push('b');
mp.push('e');
cout<< "poping element ";
while(!mp.empty())
{
cout<< ' ' <<mp.top();
mp.pop();
}
return 0;
}
Output:
Output
poping element e d c b a
Example 2
Example
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main()
{
priority_queue<string> mp;
mp.push("my");
mp.push("india");
mp.push("Is");
mp.push("Great");
mp.push("Country");
cout<< "poping element: ";
while(!mp.empty())
{
cout<< ' ' <<mp.top();
mp.pop();
}
return 0;
}
Output:
Output
poping element: my india Is Great Country