C++ std::map emplace() Function

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

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.

C++
auto r = m.emplace(3, "three");     // key 3, value "three"
bool added = r.second;              // true if the key was new
CallWhat 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
C++
template<class... Args>
std::pair<iterator, bool> emplace(Args&&... args);   // C++11

Parameters

ParameterDescription
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 ValueDescription
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.

C++ Examples

Example 1 — emplace() Adds a Pair

Add two pairs and read the flag that emplace() returns.

C++
#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;
}

How It Works

  1. emplace(3, "three") passes the key and value straight to the pair constructor, so no separate pair object is written in your code.
  2. Both keys are new, so both calls return true.
  3. 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.

C++
#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;
}

How It Works

  1. The first emplace() adds cat with value 1.
  2. The second call uses the same key, so the map is left unchanged and second is false. The value 99 is discarded.
  3. 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.

C++
#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;
}

How It Works

  1. The loop visits each name with its index i (0, 1, 2).
  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.
  3. 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

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.
insert() takes a ready pair. emplace() takes the arguments for the pair and builds it in place, which can avoid a copy or move.
No. If the key exists, the old value stays and the returned bool is false. Use insert_or_assign() to update.
C++11.
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

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