C++ std::unordered_set emplace() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_set

Definition

In C++11 and later, std::unordered_set::emplace() builds a value in place inside the unordered_set, using the arguments you give. You do not create a temporary object first, so a copy or a move can be avoided.

Like insert(), it keeps the unordered_set sorted and unique. If an equal value already exists, the new one is thrown away. It returns a pair of an iterator and a bool. Include <unordered_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Pass the arguments that the value type needs for its constructor.

C++
std::unordered_set<std::string> names;
auto r = names.emplace("Raj");       // builds the string in place
bool added = r.second;               // true

std::unordered_set<std::pair<int, int>> ps;
ps.emplace(1, 2);                    // builds pair(1, 2)
CallWhat it does
s.emplace(args...)Builds a value from args. Returns pair<iterator, bool>.

For simple types such as int, emplace(5) and insert(5) do the same work. The benefit shows with larger types such as strings or structs. One catch is that an unordered_set may create the node before it checks for a duplicate, so a repeated value can still cost a construction.

Library declarations
C++
template<class... Args>
std::pair<iterator, bool> emplace(Args&&... args);   // C++11

Parameters

ParameterDescription
args...Arguments passed to the constructor of the value type. For a std::unordered_set<int> that is just the number.

Return Value

Return ValueDescription
pair<iterator, bool>first points to the stored value. second is true if the value was added, and false if an equal value was already there.

C++ Examples

Example 1 — emplace() Builds a String in Place

Add three names, one of them twice, and look at the results.

C++
#include <iostream>
#include <unordered_set>
#include <string>

int main() {
    std::unordered_set<std::string> names;

    auto a = names.emplace("Raj");
    auto b = names.emplace("Mia");
    auto c = names.emplace("Raj");

    std::cout << std::boolalpha;
    std::cout << a.second << ' ' << b.second << ' ' << c.second << '\n';
    for (const std::string& n : names) {
        std::cout << n << '\n';
    }
    return 0;
}

How It Works

  1. emplace("Raj") builds the std::string straight inside the unordered_set from the text. The result says true, because the name is new.
  2. emplace("Mia") is also new, so it is true. The second emplace("Raj") finds an equal name already stored and returns false.
  3. The loop prints the two stored names in an unspecified order, Mia then Raj. The unordered_set has two names, even though emplace() was called three times.

Example 2 — emplace() with Two Arguments

Give emplace() two numbers and it builds a pair from them. Pairs are not sorted by the first number, then the second.

C++
#include <iostream>
#include <unordered_set>
#include <utility>

int main() {
    std::unordered_set<std::pair<int, int>> points;

    points.emplace(2, 1);
    points.emplace(1, 5);
    points.emplace(1, 2);
    auto again = points.emplace(1, 5);

    std::cout << "repeat added: " << std::boolalpha << again.second << '\n';
    for (const auto& p : points) {
        std::cout << "(" << p.first << ", " << p.second << ")\n";
    }
    return 0;
}

How It Works

  1. emplace(2, 1) passes two arguments to the std::pair constructor. There is no need to write std::make_pair.
  2. A pair compares its first value first and its second value next. So (1, 2) comes before (1, 5), and both come before (2, 1).
  3. The last emplace(1, 5) repeats a stored pair, so second is false and only three pairs are printed.

Example 3 — emplace() and a Custom Comparison

Store tasks sorted by priority. Two tasks with the same priority count as the same value.

C++
#include <iostream>
#include <unordered_set>
#include <string>

struct Task {
    int priority;
    std::string name;

    Task(int p, const std::string& n) : priority(p), name(n) {}
    bool operator<(const Task& other) const {
        return priority < other.priority;
    }
};

int main() {
    std::unordered_set<Task> tasks;

    tasks.emplace(2, "test");
    tasks.emplace(1, "write");
    tasks.emplace(2, "deploy");

    for (const Task& t : tasks) {
        std::cout << t.priority << " " << t.name << '\n';
    }
    return 0;
}

How It Works

  1. Task has a constructor with two parameters, so emplace(2, "test") can build it directly inside the unordered_set.
  2. The unordered_set orders tasks with operator<, which looks only at priority. The tasks sort as priority 1 then priority 2.
  3. emplace(2, "deploy") has the same priority as "test". Neither is smaller, so the unordered_set treats them as equal and drops the new one. That is why deploy never prints.

Frequently Asked Questions

insert() takes an existing value. emplace() takes constructor arguments and builds the value inside the unordered_set. For simple types the result is the same.
C++11. It is available in every modern compiler.
No. If an equal value is already stored, the new one is discarded and the second part of the returned pair is false.
A pair. The first part is an iterator to the stored value and the second part is a bool that is true when the value was added.
Not always. For int and other small types there is no difference. It helps most with larger types where building a temporary would be costly.

Next: emplace_hint()

Keep going through the std::unordered_set functions. Build a value in place, using a hint for its position (C++11).

emplace_hint() 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