Home C++ Sets Functions find() C++ std::set find() Function Definition In C++, std::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 set, find() returns end(). Always compare with end() before you use the result. The search never adds a value. 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 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 a 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 comparator 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. A 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 <set>
#include <string>
int main() {
std::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 set is not changed.Example 2 — find() Then erase() Find a value and remove it in one search.
#include <iostream>
#include <set>
#include <string>
int main() {
std::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 sorted 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 set.
#include <iostream>
#include <set>
#include <string>
bool hasName(const std::set<std::string>& s, const std::string& name) {
return s.find(name) != s.end();
}
int main() {
std::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 set by const reference. find() works on a const set, and a 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::set::find() return? An iterator to the value, or end() if the value is not in the 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 a 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 a set? It takes logarithmic time because a set is a balanced tree. For a million values, that is about 20 comparisons.
Next: get_allocator() Keep going through the std::set functions. Get a copy of the allocator the 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.
13 people found this page helpful
Helpful Share Copy link Suggestion