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)
Call
What 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.
Arguments passed to the constructor of the value type. For a std::unordered_set<int> that is just the number.
Return Value
Return Value
Description
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;
}
Output
true true false
Mia
Raj
How It Works
emplace("Raj") builds the std::string straight inside the unordered_set from the text. The result says true, because the name is new.
emplace("Mia") is also new, so it is true. The second emplace("Raj") finds an equal name already stored and returns false.
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.
Task has a constructor with two parameters, so emplace(2, "test") can build it directly inside the unordered_set.
The unordered_set orders tasks with operator<, which looks only at priority. The tasks sort as priority 1 then priority 2.
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.