C++ std::unordered_set find() Function

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

Definition

In C++, std::unordered_set::find() searches for a value and returns an iterator to it. Through the iterator you can read *it.

If the value is not in the unordered_set, find() returns end(). Always compare with end() before you use the result. The search never adds a value. 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 value and compare the result with end().

C++
auto it = s.find("Raj");
if (it != s.end()) {
    std::cout << *it;
}
CallWhat it does
s.find(value)Returns an iterator to value, or s.end() if it is not found.
cs.find(value)On a const set it returns a const_iterator. For an unordered_set every iterator is already read-only for the value.

Never write *s.find(v) without checking. When the value is missing, find() returns end(), and dereferencing end() is undefined behavior.

Library declarations
C++
iterator find(const key_type& value);
const_iterator find(const key_type& value) const;
template<class K> iterator find(const K& x);   // transparent hash/eq

Parameters

ParameterDescription
valueThe value to search for.

Return Value

Return ValueDescription
iteratorAn iterator to the matching value, or end() when none matches. For a const set it is a const_iterator.

C++ Examples

Example 1 — find() Returns an Iterator

Search for a value that exists and one that does not.

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

int main() {
    std::unordered_set<std::string> names = {"Mia", "Raj", "Zoe"};

    auto it = names.find("Raj");
    if (it != names.end()) {
        std::cout << "found: " << *it << '\n';
    }

    if (names.find("Tom") == names.end()) {
        std::cout << "Tom was not found\n";
    }
    return 0;
}

How It Works

  1. find("Raj") searches the sorted values and returns an iterator to "Raj". It is not end(), so it is safe to use.
  2. *it reads the stored value "Raj". There is no separate key and value, because the value is its own key.
  3. find("Tom") finds nothing, so it returns names.end(). The comparison is true and the message prints. The unordered_set is not changed.

Example 2 — find() Then erase()

Find a value and remove it in one search.

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

int main() {
    std::unordered_set<std::string> team = {"Ann", "Ben", "Cy"};

    auto it = team.find("Ben");
    if (it != team.end()) {
        team.erase(it);
    }

    for (const std::string& name : team) {
        std::cout << name << '\n';
    }
    return 0;
}

How It Works

  1. find("Ben") returns an iterator to that value. Checking against end() makes sure the erase is safe.
  2. erase(it) removes that one element. Using the iterator avoids a second search that erase("Ben") would do.
  3. The remaining values print in an unspecified order: Ann, then Cy.

Example 3 — find() in a Helper

Write a small function that returns true when a value is present, without changing the unordered_set.

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

bool hasName(const std::unordered_set<std::string>& s, const std::string& name) {
    return s.find(name) != s.end();
}

int main() {
    std::unordered_set<std::string> guests = {"apple", "pear"};

    std::cout << std::boolalpha;
    std::cout << "apple: " << hasName(guests, "apple") << '\n';
    std::cout << "kiwi: " << hasName(guests, "kiwi") << '\n';
    std::cout << "size: " << guests.size() << '\n';
    return 0;
}

How It Works

  1. The function takes the unordered_set by const reference. find() works on a const set, and an unordered_set has no operator[] that could insert.
  2. For "apple", find() succeeds and the helper returns true. For "kiwi" it returns end(), so the helper gives false.
  3. The size is still 2, which proves that looking up a missing value with find() adds nothing.

Frequently Asked Questions

An iterator to the value, or end() if the value is not in the unordered_set.
Compare the result with s.end(). If it is not equal to end(), the value was found.
No. Elements in an unordered_set are const. To change one, erase the old value and insert the new one.
count() only says whether the value exists (0 or 1). find() gives an iterator so you can read or erase it without searching again.
It takes average constant time because an unordered_set is a hash table. For a million values, that is about 20 comparisons.

Next: get_allocator()

Keep going through the std::unordered_set functions. Get a copy of the allocator the unordered_set uses for its nodes.

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

14 people found this page helpful