C++ std::priority_queue::emplace() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++11

Definition

In C++11, emplace() adds a new element to a std::priority_queue by building it inside the queue. You pass the constructor arguments, not a finished object, and the queue creates the element itself.

This is handy for pairs, tuples, and structs. Instead of pq.push(std::make_pair(3, "low")) you write pq.emplace(3, "low"). The result is the same: the new element is placed by priority, and top() still gives the highest one.

This function is a member function of std::priority_queue<T>, declared in the <queue> header. It needs C++11 or newer (-std=c++11). You call it with a dot, for example pq.emplace(...).

Syntax

Give emplace() the arguments for the constructor of the element type. It needs C++11 or newer.

C++
std::priority_queue<std::pair<int, std::string>> pq;

pq.emplace(2, "write");     // builds pair<int, string>(2, "write")
pq.emplace(8, "deploy");

auto best = pq.top();       // {8, "deploy"}
CallWhat it does
pq.emplace(args...)Builds a new element from args inside the queue, then restores the heap order.

emplace() returns nothing and takes O(log n) time, like push(). The arguments must match a constructor of the element type. For simple types such as int, push() and emplace() behave the same.

Library declarations
C++
template<class... Args>
void emplace(Args&&... args);   // C++11

// priority_queue<T, Container, Compare> from <queue>
// It calls c.emplace_back(std::forward<Args>(args)...)
// and then std::push_heap(...).

Parameters

ParameterDescription
args...Zero or more arguments that are passed to the constructor of the element type T. The queue builds the new element from them.

Return Value

Return ValueDescription
Nothingemplace() returns void.

C++ Examples

Example 1 — Emplace Pairs Without make_pair

The first value of a pair is the priority. Pass both values straight to emplace().

C++
#include <iostream>
#include <queue>
#include <string>
#include <utility>

int main() {
    std::priority_queue<std::pair<int, std::string>> pq;

    pq.emplace(2, "write");
    pq.emplace(8, "deploy");
    pq.emplace(5, "review");

    while (!pq.empty()) {
        std::cout << pq.top().first << " " << pq.top().second << '\n';
        pq.pop();
    }
    return 0;
}

How It Works

  1. emplace(2, "write") passes 2 and "write" to the pair constructor. No temporary std::make_pair is needed.
  2. Pairs compare the first number first, so 8 beats 5, and 5 beats 2. The queue puts 8 on top.
  3. The loop prints the pairs from the highest number to the lowest: deploy, review, write.

Example 2 — Emplace a Struct with a Constructor

When a struct has a constructor, emplace() calls it for you. Add operator< so the queue knows the order.

C++
#include <iostream>
#include <queue>

struct Job {
    int id;
    int priority;

    Job(int i, int p) : id(i), priority(p) {}

    bool operator<(const Job& other) const {
        return priority < other.priority;
    }
};

int main() {
    std::priority_queue<Job> jobs;

    jobs.emplace(1, 3);
    jobs.emplace(2, 9);
    jobs.emplace(3, 6);

    std::cout << "Top job id: " << jobs.top().id
              << " (priority " << jobs.top().priority << ")\n";
    std::cout << "Jobs waiting: " << jobs.size() << '\n';
    return 0;
}

How It Works

  1. emplace(1, 3) calls Job(1, 3) inside the queue. The code does not write Job three times.
  2. operator< compares the priority numbers. Job 2 has priority 9, the largest, so it ends up on top.
  3. The output shows id 2 for the top job. All three jobs are still waiting, so size() prints 3.

Example 3 — Min-Heap Pairs: Shortest Distance First

This pattern is common in shortest-path code. Store (distance, node) and always take the smallest distance.

C++
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

int main() {
    using Item = std::pair<int, int>;   // (distance, node)
    std::priority_queue<Item, std::vector<Item>, std::greater<Item>> pq;

    pq.emplace(7, 1);
    pq.emplace(3, 2);
    pq.emplace(5, 3);

    while (!pq.empty()) {
        std::cout << "distance " << pq.top().first
                  << " -> node " << pq.top().second << '\n';
        pq.pop();
    }
    return 0;
}

How It Works

  1. using Item = std::pair<int, int> gives the pair a short name. The queue type adds std::greater<Item> to make a min-heap.
  2. emplace(7, 1) and the next two calls build the pairs in place. The pair with the smallest distance is kept on top.
  3. The loop prints the shortest distance first: 3, 5, 7. Each pop() reveals the next smallest pair.

Frequently Asked Questions

It builds a new element inside the priority queue from constructor arguments, then restores the heap order. The highest-priority element is still on top.
push() takes a finished object. emplace() takes the constructor arguments and builds the object in place, which can save a copy or a move. For plain numbers they behave the same.
emplace() was added in C++11. Every modern compiler supports it. Compile with -std=c++11 or newer.
No. For priority_queue it returns void. You read the top element later with top().
Yes. Create the queue with std::greater, for example std::priority_queue<std::pair<int,int>, std::vector<std::pair<int,int>>, std::greater<std::pair<int,int>>>, and call emplace() as usual.

Next: empty()

Check if the queue has no elements. It returns true or false with std::priority_queue.

empty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful