C++ std::unordered_set emplace_hint() Function

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

Definition

In C++11 and later, std::unordered_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 average constant time. A wrong hint is safe, and the unordered_set only does a normal search. It returns a plain iterator. Include <unordered_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Pass the hint iterator first, then the constructor arguments.

C++
std::unordered_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
CallWhat it does
s.emplace_hint(hint, args...)Builds a value from args. The value should go just before hint. 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.

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

Parameters

ParameterDescription
hintAn 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 ValueDescription
iteratorAn iterator to the new value, or to the equal value that was already in the unordered_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 <unordered_set>

int main() {
    std::unordered_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;
}

How It Works

  1. The loop makes 10, 20, 30, 40, each bigger than the last. Every new value belongs at the very end of the unordered_set.
  2. 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.
  3. The unordered_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 <unordered_set>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> fruit = {"apple", "banana", "cherry"};
    std::unordered_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;
}

How It Works

  1. The hint starts at end(). The fruit are already in an unspecified order, so each one belongs after the last.
  2. 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.
  3. 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.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> s = {10, 20, 30};

    auto it = s.emplace_hint(s.begin(), 20);

    std::cout << "returned: " << *it << '\n';
    std::cout << "size: " << s.size() << '\n';
    std::cout << "same position: " << std::boolalpha << (it == s.find(20)) << '\n';
    return 0;
}

How It Works

  1. 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.
  2. The unordered_set already has 20, so nothing is added and the size stays 3. The call returns an iterator to the existing 20.
  3. 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 unordered_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.

Next: empty()

Keep going through the std::unordered_set functions. Check whether an unordered_set has no values at all.

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