priority_queue emplace() in C++ STL

Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is the greatest of all elements in the queue.

priority_queue::emplace()

This function is used to insert a new element into the priority queue container, the new element is added to the top of the priority queue.
Syntax :

priorityqueuename.emplace(value)
Parameters :
The element to be inserted into the priority
queue is passed as the parameter.
Result :
The parameter is added to the
priority queue at the top position.

Examples:



Input  : mypqueue{5, 4};
         mypqueue.emplace(6);
Output : mypqueue = 6, 5, 4

Input  : mypqueue{};
         mypqueue.emplace(4);
Output : mypqueue = 4

Note: In priority_queue container, the elements are printed in reverse order because the top is printed first then moving on to other elements.


Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
2. Parameter should be of same type as that of the container, otherwise an error is thrown.

filter_none

edit
close

play_arrow

link
brightness_4
code

// INTEGER PRIORITY QUEUE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    priority_queue<int> mypqueue;
    mypqueue.emplace(1);
    mypqueue.emplace(2);
    mypqueue.emplace(3);
    mypqueue.emplace(4);
    mypqueue.emplace(5);
    mypqueue.emplace(6);
    // queue becomes 1, 2, 3, 4, 5, 6
  
    // printing the priority queue
    cout << "mypqueue = ";
    while (!mypqueue.empty()) {
        cout << mypqueue.top() << " ";
        mypqueue.pop();
    }
  
    return 0;
}

chevron_right


Output:

mypqueue = 6 5 4 3 2 1
filter_none

edit
close

play_arrow

link
brightness_4
code

// CHARACTER PRIORITY QUEUE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    priority_queue<char> mypqueue;
    mypqueue.emplace('A');
    mypqueue.emplace('b');
    mypqueue.emplace('C');
    mypqueue.emplace('d');
    mypqueue.emplace('E');
    mypqueue.emplace('f');
    // queue becomes A, b, C, d, E, f
  
    // printing the priority queue
    cout << "mypqueue = ";
    while (!mypqueue.empty()) {
        cout << mypqueue.top() << " ";
        mypqueue.pop();
    }
  
    return 0;
}

chevron_right


Output:

mypqueue = f d b E C A
filter_none

edit
close

play_arrow

link
brightness_4
code

// STRING PRIORITY QUEUE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <queue>
#include <string>
using namespace std;
  
int main()
{
    priority_queue<string> mypqueue;
    mypqueue.emplace("portal");
    mypqueue.emplace("computer science");
    mypqueue.emplace("is a");
    mypqueue.emplace("GEEKSFORGEEKS");
    // queue becomes portal, computer scince,
    // is a, GEEKSFORGEEKS
  
    // printing the priority queue
    cout << "mypqueue = ";
    while (!mypqueue.empty()) {
        cout << mypqueue.top() << " ";
        mypqueue.pop();
    }
  
    return 0;
}

chevron_right


Output:

mypqueue = portal is a computer science GEEKSFORGEEKS

Application :
Given a number of integers, add them to the priority queue using emplace() and find the size of the priority queue.

Input : 5, 13, 0, 9, 4
Output: 5

Algorithm
1. Insert the given elements to the priority queue container one by one using emplace().
2. Keep popping the elements of priority queue until it becomes empty, and increment the counter variable.
3. Print the counter variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Application of emplace() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    int c = 0;
  
    // Empty Priority Queue
    priority_queue<int> pqueue;
  
    // inserting elements into priority_queue
    pqueue.emplace(5);
    pqueue.emplace(13);
    pqueue.emplace(0);
    pqueue.emplace(9);
    pqueue.emplace(4);
  
    // Priority queue becomes 13, 9, 5, 4, 0
  
    // Counting number of elements in queue
    while (!pqueue.empty()) {
        pqueue.pop();
        c++;
    }
    cout << c;
}

chevron_right


Output:

5

Time Complexity : O(1)

emplace() vs push()
When we use push(), we create an object and then insert it into the priority_queue. With emplace(), the object is constructed in-place and saves an unnecessary copy. Please see emplace vs insert in C++ STL for details.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to demonstrate difference between
// emplace and insert
#include<bits/stdc++.h>
using namespace std;
    
int main()
{
    // declaring priority queue
    priority_queue<pair<char, int>> pqueue;
        
    // using emplace() to insert pair in-place
    pqueue.emplace('a', 24);
        
    // Below line would not compile
    // pqueue.push('b', 25);    
        
    // using push() to insert pair 
    pqueue.push(make_pair('b', 25));    
        
    // printing the priority_queue
    while (!pqueue.empty()) {
        pair<char, int> p =  pqueue.top();
        cout << p.first << " "
             << p.second << endl;
        pqueue.pop();
    }
    
    return 0;
}

chevron_right


Output:

b 25
a 24


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.