Home C++ Maps Functions emplace_hint() C++ std::map emplace_hint() Function 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.
auto it = m.emplace_hint(m.end(), 4, "four"); // key 4 goes last
std::cout << it->first; // 4 Call What 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 template<class... Args>
iterator emplace_hint(const_iterator hint, Args&&... args); // C++11 Parameters Parameter Description 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 Value Description iteratorAn iterator to the new pair. If the key already existed, an iterator to the existing pair and nothing is inserted.
Important
emplace_hint() returns only an iterator , so you cannot tell from the result whether a pair was added. It does not overwrite an existing key. A wrong hint costs time but never gives wrong results, so the map stays sorted.
C++ Examples 1. Append with end() as the hint 2. Hint with an existing key 3. A wrong hint is still correct Example 1 — emplace_hint() with end() Add keys in ascending order, using m.end() as the hint every time.
#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 The keys 1 to 5 grow, so every new pair belongs at the very end of the map. 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.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.
#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;
} key: 2, value: two
size: 2 How It Works The map already has key 2 with the value "two". 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.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.
#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 Every call uses m.begin() as the hint, but the keys 30, 10, 20 do not belong in the same place each time. 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. The loop prints 10 20 30: the hint cannot break the sorted order. Frequently Asked Questions What does std::map::emplace_hint() do? It builds a pair in place, like emplace(), but it takes an iterator hint for the position so that the insert can be faster.
What should I use as the hint? The iterator just after where the new key belongs. For keys added in ascending order that is m.end().
What if my hint is wrong? The result is still correct and the map stays sorted. The insert is just a bit slower.
What does emplace_hint() return? An iterator to the new pair, or to the existing pair if the key was already there. It does not return a bool.
Does emplace_hint() overwrite an existing value? 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 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.
8 people found this page helpful
Helpful Share Copy link Suggestion