A priority queue is a dynamically resizing container adopter in the C++ Standard Library. Elements in a priority queue are arranged in non-decreasing order such that the first element is always the greatest element in the queue. The priority_queue::emplace()
is a method available in the STL which inserts a new element to the priority queue.
Note:
emplace takes the arguments necessary to construct an object in place, whereas push takes a reference to an object.
Syntax:
priorityq_name.emplace(args);
Parameters: The priority_queue::emplace()
accepts the element to be inserted into the queue
Return value: none
Example of priority_queue::emplace() method
#include <iostream> #include <queue> using namespace std; void print_q(priority_queue <int> q) //helper function to print the contents of a priority queue { priority_queue <int> temp = q; while (!temp.empty()) { cout<<temp.top()<<"\t"; temp.pop(); } cout<<endl; } int main () { priority_queue<int> q1; q1.emplace(10); q1.emplace(20); q1.emplace(30); print_q(q1); return 0; }
Output:
30 20 10