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

Priority Queue Emplace Function

BLUF: Mastering Priority Queue Emplace 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 Emplace Function

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

The emplace method in C++ priority_queue is employed to insert a fresh element into the priority-queue, positioning it at the highest point in the queue.

Syntax

Think of 'pq' as an instance of the priority_queue data structure.

Example

pq.emplace(value);

Parameter

Value: The element passed as a parameter is inserted into the priority queue.

Return value

Example 1

Example

#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main()
{
  priority_queue<string> mp; 
mp.emplace("India");
mp.emplace("Bharat");
mp.emplace("Hindustan");
cout<< "mypqueue = " ;
while (!mp.empty())
 {
		cout<<mp.top() << " ";
		mp.pop();
}
return 0;
}

Output:

Output

mypqueue = India Hindustan Bharat

Example 2

Example

#include <iostream>
#include <queue>
using namespace std;
int main()
{
  priority_queue<int> mp;
mp.emplace(1);
mp.emplace(2);
mp.emplace(3);
cout<< "mypqueue = " ;
while (!mp.empty()) 
{
cout<<mp.top() << " ";
mp.pop();
}
return 0;
}

Output:

Output

mypqueue = 3 2 1

Example 3

Example

#include <iostream>
#include <queue>
using namespace std;
int main()
{
  priority_queue<char> mp;
  mp.emplace('a');
mp.emplace('b');
mp.emplace('c');
cout<< "mypqueue = " ;
while (!mp.empty()) 
{
	cout<<mp.top() << " ";
	mp.pop();
}
	return 0;
}

Output:

Output

mypqueue = c b a

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