Home C++ Unordered Sets Functions find() C++ std::unordered_set find() Function 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().
auto it = s.find("Raj");
if (it != s.end()) {
std::cout << *it;
} Call What 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 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 Parameter Description valueThe value to search for.
Return Value Return Value Description iteratorAn iterator to the matching value, or end() when none matches. For a const set it is a const_iterator.
Important
Check against end() before you dereference. find() never inserts and never throws. The element is const, so you cannot change *it. To change a value, erase it and insert a new one. An unordered_set has no at() and no operator[].
C++ Examples 1. Find a value and read it 2. Erase through the iterator 3. A safe membership helper Example 1 — find() Returns an Iterator Search for a value that exists and one that does not.
#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;
} found: Raj
Tom was not found How It Works find("Raj") searches the sorted values and returns an iterator to "Raj". It is not end(), so it is safe to use.*it reads the stored value "Raj". There is no separate key and value, because the value is its own key.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.
#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 find("Ben") returns an iterator to that value. Checking against end() makes sure the erase is safe.erase(it) removes that one element. Using the iterator avoids a second search that erase("Ben") would do.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.
#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;
} apple: true
kiwi: false
size: 2 How It Works 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. For "apple", find() succeeds and the helper returns true. For "kiwi" it returns end(), so the helper gives false. The size is still 2, which proves that looking up a missing value with find() adds nothing. Frequently Asked Questions What does std::unordered_set::find() return? An iterator to the value, or end() if the value is not in the unordered_set.
How do I know if find() found the value? Compare the result with s.end(). If it is not equal to end(), the value was found.
Can I change a value through the iterator from find()? No. Elements in an unordered_set are const. To change one, erase the old value and insert the new one.
What is the difference between find() and count()? count() only says whether the value exists (0 or 1). find() gives an iterator so you can read or erase it without searching again.
How fast is find() on an unordered_set? 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 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.
14 people found this page helpful
Helpful Share Copy link Suggestion