C++ priority_queue push method is employed for adding an element to a priority queue data structure. This action results in the increment of the priority queue's size by one as the new element is appended to the end. Subsequently, the elements within the priority queue automatically reorganize based on their priority levels.
Syntax
Consider 'pq' as an object of the priority_queue class.
pq.push (value);
Parameter
Value: This is employed to insert a value into a priority queue.
Return value
Example 1
#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:
poping element e d c b a
Example 2
#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:
poping element: my india Is Great Country