Home C++ Sets Functions contains() C++ std::set contains() Function Definition In C++20, std::set::contains() answers one question: is this value in the 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 <set> and use -std=c++20 or newer.
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. 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 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 set contains this value. falseThe 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 <set>
#include <string>
int main() {
std::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 set is not changed in either case.Example 2 — contains() in an if Statement Check a username before showing a message.
#include <iostream>
#include <set>
#include <string>
int main() {
std::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 set. "ann" is stored, so contains() is true and the registered message prints."cy" is not stored, so the else branch runs. The 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 <set>
int main() {
std::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 set, so all three say true. Value 7 is not in the set, so all three say false. Pick contains() when you can, because it is the shortest and clearest. Frequently Asked Questions What does std::set::contains() do? It returns true if the 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. A 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::set they take the same logarithmic time. contains() is mostly about clearer code.
Next: count() Keep going through the std::set functions. Tell whether a value exists: the answer is 0 or 1 for a 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.
11 people found this page helpful
Helpful Share Copy link Suggestion