Home C++ Maps Functions emplace() C++ std::map emplace() Function Definition In C++11, std::map::emplace() builds a new pair directly inside the map from the arguments you pass. You do not make a pair yourself first, so a copy or move can be avoided.
Like insert(), it adds the pair only if the key is new . If the key exists, the map is not changed. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs . Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Pass the arguments for the key and the value, in that order.
auto r = m.emplace(3, "three"); // key 3, value "three"
bool added = r.second; // true if the key was new Call What it does m.emplace(args...)Constructs a pair from args. Returns std::pair<iterator, bool>.
emplace() may build the pair before it checks the key. If the key exists, that pair is destroyed again. When you want no wasted work, use try_emplace() (C++17).
Library declarations template<class... Args>
std::pair<iterator, bool> emplace(Args&&... args); // C++11 Parameters Parameter Description args...The arguments used to construct the std::pair<const Key, T>. Normally one argument for the key and one for the value.
Return Value Return Value Description pair<iterator, bool>first points to the pair with that key. second is true if a new pair was inserted and false if the key already existed.
Important
emplace() does not overwrite an existing value. It also may construct the pair first and then discard it when the key exists, so it can still cost something. It is C++11 . For update-or-insert use insert_or_assign(), and remember m[key] inserts a default value when the key is missing.
C++ Examples 1. Add a pair in place 2. Emplace with a key that exists 3. Fill a map in a loop Example 1 — emplace() Adds a Pair Add two pairs and read the flag that emplace() returns.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m;
std::cout << std::boolalpha;
auto r1 = m.emplace(3, "three");
auto r2 = m.emplace(1, "one");
std::cout << "3 added: " << r1.second << '\n';
std::cout << "1 added: " << r2.second << '\n';
std::cout << "first key: " << m.begin()->first << '\n';
return 0;
} 3 added: true
1 added: true
first key: 1 How It Works emplace(3, "three") passes the key and value straight to the pair constructor, so no separate pair object is written in your code.Both keys are new, so both calls return true. 3 was added before 1, but the map keeps its keys sorted. begin() therefore points at key 1.Example 2 — emplace() and an Existing Key Emplace a key twice and see that the first value stays.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> m;
m.emplace("cat", 1);
auto r = m.emplace("cat", 99);
std::cout << "added: " << std::boolalpha << r.second << '\n';
std::cout << "cat = " << r.first->second << '\n';
std::cout << "size = " << m.size() << '\n';
return 0;
} added: false
cat = 1
size = 1 How It Works The first emplace() adds cat with value 1. The second call uses the same key, so the map is left unchanged and second is false. The value 99 is discarded. r.first points to the old pair, so cat = 1 is printed, and the size is still 1.Example 3 — emplace() in a Loop Number a list of names by building one pair per name.
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
std::vector<std::string> names = {"Ann", "Bob", "Cy"};
std::map<int, std::string> ids;
for (std::size_t i = 0; i < names.size(); ++i) {
ids.emplace(static_cast<int>(i) + 1, names[i]);
}
for (const auto& kv : ids) {
std::cout << kv.first << " -> " << kv.second << '\n';
}
return 0;
} 1 -> Ann
2 -> Bob
3 -> Cy How It Works The loop visits each name with its index i (0, 1, 2). ids.emplace(i + 1, names[i]) makes a pair with the key i + 1 and a copy of the name, directly inside the map.The keys 1, 2, 3 are already in order, so the final loop prints the names in the same order as the vector. Frequently Asked Questions What does std::map::emplace() do? It constructs a new key and value pair inside the map from the arguments you pass. If the key already exists, it does not change the map.
What is the difference between emplace() and insert()? insert() takes a ready pair. emplace() takes the arguments for the pair and builds it in place, which can avoid a copy or move.
Does emplace() overwrite an existing value? No. If the key exists, the old value stays and the returned bool is false. Use insert_or_assign() to update.
Which C++ version has emplace()? C++11.
When should I use try_emplace() instead? Use try_emplace() (C++17) when building the value is costly or when you pass something you moved. It does not touch the arguments if the key exists.
Next: emplace_hint() Keep going through the std::map functions. Build a pair in place, using a hint for where it should go (C++11).
emplace_hint() tutorial → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion