C++ std::set emplace() Function

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

Definition

In C++11 and later, std::set::emplace() builds a value in place inside the 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 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 <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

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

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

std::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 a 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::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 <set>
#include <string>

int main() {
    std::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 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 sorted order, Mia then Raj. The 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 sorted by the first number, then the second.

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

int main() {
    std::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 <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::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 set.
  2. The 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 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 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::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.

15 people found this page helpful