Home C++ Unordered Sets Functions contains() C++ std::unordered_set contains() Function Definition In C++20, std::unordered_set::contains() answers one question: is this value in the unordered_set? It returns true or false.
It does not add the value. It replaces the older style s.count(value) > 0 or s.find(value) != s.end() with a name that says exactly what you mean. Include <unordered_set> and use -std=c++20 or newer.
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. Use the bool result in an if.
if (s.contains("Raj")) {
// the value exists
}
bool has = s.contains("Zoe"); Call What it does s.contains(value)Returns true if the value is in the unordered_set, and false if it is not.
If your compiler is older than C++20, write s.count(value) != 0 or s.find(value) != s.end(). They give the same answer.
Library declarations bool contains(const key_type& value) const; // C++20
template<class K> bool contains(const K& x) const; Parameters Parameter Description valueThe value to look for.
Return Value Return Value Description trueThe unordered_set contains this value. falseThe unordered_set does not contain this value.
C++ Examples 1. Check for a value 2. Use it in a condition 3. Same answer as count and find Example 1 — contains() Returns a Bool Print the result of contains() for a present and a missing value.
#include <iostream>
#include <unordered_set>
#include <string>
int main() {
std::unordered_set<std::string> names = {"Mia", "Raj"};
std::cout << std::boolalpha;
std::cout << "Raj: " << names.contains("Raj") << '\n';
std::cout << "Zoe: " << names.contains("Zoe") << '\n';
return 0;
} How It Works boolalpha makes the program print true and false instead of 1 and 0.contains("Raj") finds the value and returns true.contains("Zoe") does not find it and returns false. The unordered_set is not changed in either case.Example 2 — contains() in an if Statement Check a username before showing a message.
#include <iostream>
#include <unordered_set>
#include <string>
int main() {
std::unordered_set<std::string> users = {"ann", "ben"};
for (const std::string name : {"ann", "cy"}) {
if (users.contains(name)) {
std::cout << name << " is registered\n";
} else {
std::cout << name << " is unknown\n";
}
}
return 0;
} ann is registered
cy is unknown How It Works The loop tries two names: "ann" and "cy". Each is checked against the unordered_set. "ann" is stored, so contains() is true and the registered message prints."cy" is not stored, so the else branch runs. The unordered_set still has only two names.Example 3 — contains() Compared with count() and find() Three ways to ask the same question, with the same answers.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s = {1, 2, 3};
std::cout << std::boolalpha;
for (int value : {2, 7}) {
bool a = s.contains(value);
bool b = s.count(value) > 0;
bool c = s.find(value) != s.end();
std::cout << value << ": " << a << " " << b << " " << c << '\n';
}
return 0;
} 2: true true true
7: false false false How It Works For each value, three tests are run: contains() (C++20), count() > 0, and find() != end(). Value 2 is in the unordered_set, so all three say true. Value 7 is not in the unordered_set, so all three say false. Pick contains() when you can, because it is the shortest and clearest. Frequently Asked Questions What does std::unordered_set::contains() do? It returns true if the unordered_set holds the given value and false if it does not.
Which C++ version has contains()? C++20. In older versions use count(value) != 0 or find(value) != end().
Does contains() add the value if it is missing? No. It only checks. An unordered_set never inserts during a lookup.
Should I call contains() and then find()? It works, but it searches twice. If you need the iterator, call find() once.
Is contains() faster than count()? For a std::unordered_set they take the same average constant time. contains() is mostly about clearer code.
Next: count() Keep going through the std::unordered_set functions. Tell whether a value exists: the answer is 0 or 1 for an unordered_set.
count() 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