Priority Queue Push Function - C++ Programming Tutorial
C++ Course / Data Structures / Priority Queue Push Function

Priority Queue Push Function

BLUF: Mastering Priority Queue Push Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Priority Queue Push Function

C++ is renowned for its efficiency. Learn how Priority Queue Push Function enables low-level control and high-performance computing in the tutorial below.

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.

Example

pq.push (value);

Parameter

Value: This is employed to insert a 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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience