In C++11 and later, std::set::emplace_hint() builds a value in place like emplace(), but it also takes a hint: an iterator that says where the value probably goes.
If the hint is right, the insert can finish in amortized constant time instead of logarithmic time. A wrong hint is safe, and the set only does a normal search. It returns a plain iterator. Include <set> to use it.
This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.
Syntax
Pass the hint iterator first, then the constructor arguments.
C++
std::set<int> s;
auto it = s.emplace_hint(s.end(), 10); // hint: the value goes at the end
it = s.emplace_hint(it, 20); // reuse the result as the next hint
Call
What it does
s.emplace_hint(hint, args...)
Builds a value from args. The value should go just beforehint. Returns an iterator to the stored value.
The best hint is the iterator just after where the value will be. When you add sorted data, use s.end() every time, or use the iterator returned by the last call. Unlike emplace(), the result is an iterator only, not a pair, so you cannot tell if the value was new from the return.
An iterator to the position that the new value should be placed before. It is only a hint.
args...
Arguments for the constructor of the value type.
Return Value
Return Value
Description
iterator
An iterator to the new value, or to the equal value that was already in the set.
C++ Examples
Example 1 — emplace_hint() with end()
Add values that arrive already in order. The end is always the right hint.
C++
#include <iostream>
#include <set>
int main() {
std::set<int> s;
for (int v = 10; v <= 40; v += 10) {
auto it = s.emplace_hint(s.end(), v);
std::cout << "added " << *it << '\n';
}
std::cout << "size: " << s.size() << '\n';
return 0;
}
Output
added 10
added 20
added 30
added 40
size: 4
How It Works
The loop makes 10, 20, 30, 40, each bigger than the last. Every new value belongs at the very end of the set.
So s.end() is the correct hint every time, and each insert is quick. The returned iterator it points to the value that was just stored, so *it prints it.
The set ends with four values. A loop like this is a good fit for data that is already sorted.
Example 2 — emplace_hint() Result as the Next Hint
Use each returned iterator as the hint for the next value.
C++
#include <iostream>
#include <set>
#include <string>
#include <vector>
int main() {
std::vector<std::string> fruit = {"apple", "banana", "cherry"};
std::set<std::string> s;
auto hint = s.end();
for (const std::string& f : fruit) {
hint = s.emplace_hint(hint, f);
++hint;
}
for (const std::string& f : s) {
std::cout << f << '\n';
}
return 0;
}
Output
apple
banana
cherry
How It Works
The hint starts at end(). The fruit are already in alphabetical order, so each one belongs after the last.
After each call, hint holds an iterator to the value just stored. Then ++hint moves it one step forward, to the position right after that value, which is the best hint for the next fruit.
The final loop prints apple, banana, cherry. The hint never changes the result, only how fast the work is done.
Example 3 — emplace_hint() and Duplicates
Add a value that is already stored and see which iterator comes back.
The hint s.begin() is a poor guess, since 20 is not the first value. A wrong hint is safe, it only costs a little time.
The set already has 20, so nothing is added and the size stays 3. The call returns an iterator to the existing20.
The last line proves it. it equals s.find(20), so both point at the same stored value. A bool is not returned, so you learn about the repeat only by checking the size or the iterator.
Frequently Asked Questions
It is an iterator that suggests where the new value will go. The value should be placed just before the hint. If the guess is right, the insert is faster.
Nothing bad. The set ignores the hint and does a normal search, so the result is still correct. It only loses the speed gain.
An iterator to the stored value, or to the existing equal value if the new one was a duplicate. It does not return a pair.
C++11 and later.
When you add many values that you know are already sorted, such as copying from a sorted vector. Use end() as the hint. For one-off inserts, plain insert() or emplace() is fine.