C++ std::map emplace_hint() Function

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

Definition

In C++11, std::map::emplace_hint() works like emplace() but you also give an iterator hint: a guess for where the new pair will end up. A good hint can make the insert faster.

The hint never changes what is stored. A bad hint is still correct, only slower. The function returns just an iterator, not a pair with a bool. 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

Give the hint first, then the arguments for the key and the value.

C++
auto it = m.emplace_hint(m.end(), 4, "four");   // key 4 goes last
std::cout << it->first;                          // 4
CallWhat it does
m.emplace_hint(hint, args...)Builds a pair from args and inserts it near hint. Returns an iterator to the new pair, or to the existing one if the key was there.

The best hint is the iterator just after the place where the new key belongs. When you add keys in ascending order, m.end() is a perfect hint every time.

Library declarations
C++
template<class... Args>
iterator emplace_hint(const_iterator hint, Args&&... args);   // C++11

Parameters

ParameterDescription
hintAn iterator to the position that will come right after the new pair. It is only a suggestion for speed.
args...The arguments used to construct the pair (key, then value).

Return Value

Return ValueDescription
iteratorAn iterator to the new pair. If the key already existed, an iterator to the existing pair and nothing is inserted.

C++ Examples

Example 1 — emplace_hint() with end()

Add keys in ascending order, using m.end() as the hint every time.

C++
#include <iostream>
#include <map>

int main() {
    std::map<int, int> squares;

    for (int i = 1; i <= 5; ++i) {
        squares.emplace_hint(squares.end(), i, i * i);
    }

    for (const auto& kv : squares) {
        std::cout << kv.first << ":" << kv.second << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The keys 1 to 5 grow, so every new pair belongs at the very end of the map.
  2. squares.end() is the position right after the last pair, which is the ideal hint here. The map does not have to search from the top.
  3. Each call builds key:value in place. The loop prints the five pairs in the order they were added.

Example 2 — emplace_hint() and an Existing Key

Use the returned iterator to see that an existing pair is not replaced.

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

int main() {
    std::map<int, std::string> m = {{1, "one"}, {2, "two"}};

    auto it = m.emplace_hint(m.begin(), 2, "TWO");

    std::cout << "key: " << it->first << ", value: " << it->second << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

How It Works

  1. The map already has key 2 with the value "two".
  2. emplace_hint(m.begin(), 2, "TWO") finds that the key exists. It does not replace the value and returns an iterator to the old pair.
  3. Reading through it shows two, not TWO, and the size is still 2.

Example 3 — emplace_hint() with a Bad Hint

Give a poor hint on purpose. The map still ends up sorted.

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

int main() {
    std::map<int, std::string> m;

    m.emplace_hint(m.begin(), 30, "thirty");
    m.emplace_hint(m.begin(), 10, "ten");
    m.emplace_hint(m.begin(), 20, "twenty");

    for (const auto& kv : m) {
        std::cout << kv.first << " ";
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Every call uses m.begin() as the hint, but the keys 30, 10, 20 do not belong in the same place each time.
  2. When a hint is wrong, the map falls back to a normal search from the root of its tree. The result is correct, only a little slower.
  3. The loop prints 10 20 30: the hint cannot break the sorted order.

Frequently Asked Questions

It builds a pair in place, like emplace(), but it takes an iterator hint for the position so that the insert can be faster.
The iterator just after where the new key belongs. For keys added in ascending order that is m.end().
The result is still correct and the map stays sorted. The insert is just a bit slower.
An iterator to the new pair, or to the existing pair if the key was already there. It does not return a bool.
No. If the key exists, nothing is changed.

Next: empty()

Keep going through the std::map functions. Check whether the map has no elements.

empty() 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.

8 people found this page helpful