Home C++ Unordered Maps Functions emplace_hint() C++ std::unordered_map emplace_hint() Function Definition In C++11, std::unordered_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 <unordered_map> to use it.
This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs . Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. 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.
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 <unordered_map>
int main() {
std::unordered_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 unordered_map. squares.end() is the position right after the last pair, which is the ideal hint here. The unordered_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 <unordered_map>
#include <string>
int main() {
std::unordered_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 unordered_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 keys are still inserted correctly.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_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");
std::cout << "size: " << m.size() << '\n';
std::cout << "10: " << m.at(10) << '\n';
std::cout << "20: " << m.at(20) << '\n';
std::cout << "30: " << m.at(30) << '\n';
return 0;
} size: 3
10: ten
20: twenty
30: thirty How It Works Every call uses m.begin() as the hint, even when that position is a poor guess for the next key. When a hint is wrong, the unordered_map falls back to a normal hash lookup. The result is correct, only a little slower. All three keys are present. Looking them up with at() keeps the printed order stable. Frequently Asked Questions What does std::unordered_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? An iterator near where you expect the key. On unordered_map hints help much less than on std::map; prefer emplace() unless you measure a benefit.
What if my hint is wrong? The result is still correct. 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::unordered_map functions. Check whether the unordered_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.
9 people found this page helpful
Helpful Share Copy link Suggestion