Home C++ Sets Functions count() C++ std::set count() Function Definition In C++, std::set::count() returns the number of times a value is stored . A std::set never holds the same value twice, so the answer is always 0 or 1 .
That makes it a simple “does this value exist?” test. It does not add the 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 you are looking for.
if (s.count("Raj") == 1) {
// value exists
}
bool has = s.count("Zoe") > 0; Call What it does s.count(value)Returns 1 if the value is in the set, and 0 if it is not.
count() only says whether the value exists. To also get an iterator to it, use find(). Since C++20, contains() expresses the same test more clearly. A std::multiset can return larger numbers, but a std::set cannot.
Library declarations size_type count(const key_type& value) const;
template<class K> size_type count(const K& x) const; // transparent comparator Parameters Parameter Description valueThe value to look for.
Return Value Return Value Description 1The value is in the set. 0The value is not in the set.
C++ Examples 1. Check values 2. Use count() in an if 3. Count how many values match Example 1 — count() Returns 0 or 1 Ask for one value that exists and one that does not.
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> names = {"Mia", "Raj"};
std::cout << "Raj: " << names.count("Raj") << '\n';
std::cout << "Zoe: " << names.count("Zoe") << '\n';
std::cout << "size: " << names.size() << '\n';
return 0;
} How It Works count("Raj") searches the sorted values and finds one match, so the result is 1.count("Zoe") finds none, so the result is 0. Because values are unique, it can never be 2 or more.The size is still 2. Asking with count() does not add "Zoe". Example 2 — count() in a Condition Add a color to a set only the first time you see it.
#include <iostream>
#include <set>
#include <string>
#include <vector>
int main() {
std::vector<std::string> input = {"red", "blue", "red", "green", "blue"};
std::set<std::string> seen;
for (const std::string& color : input) {
if (seen.count(color) == 0) {
seen.insert(color);
std::cout << "new: " << color << '\n';
}
}
std::cout << "unique: " << seen.size() << '\n';
return 0;
} new: red
new: blue
new: green
unique: 3 How It Works The loop reads each color. count(color) == 0 is true only when the color has never been stored. For a new color, insert() stores it and a message prints. The second red and the second blue find a count of 1, so they are skipped. The set ends with three unique colors. The printed lines show the order of first discovery, not sorted order. Example 3 — count() Added Up Check how many of several wanted values are present by adding the results.
#include <iostream>
#include <set>
#include <string>
#include <vector>
int main() {
std::set<std::string> menu = {"tea", "coffee", "juice"};
std::vector<std::string> wanted = {"tea", "cake", "juice", "soup"};
int available = 0;
for (const std::string& item : wanted) {
available += static_cast<int>(menu.count(item));
}
std::cout << available << " of " << wanted.size() << " items are on the menu\n";
return 0;
} 2 of 4 items are on the menu How It Works menu has three drinks. wanted lists four items, two of which are not on the menu.Each menu.count(item) is 1 for tea and juice, and 0 for cake and soup. Adding them gives 2. This only works as a sum because the answer is always 0 or 1 for a std::set. Frequently Asked Questions What does std::set::count() return? The number of times the value is stored. For a std::set that is only 0 or 1, because values are unique.
Does count() add the value to the set? No. It only looks. A set has no operator[], so you never insert by accident with a lookup.
What is the difference between count() and find()? count() only says whether the value exists. find() returns an iterator, so you can also use or erase the value without a second search.
What is the difference between count() and contains()? They give the same answer for a set, but contains() (C++20) returns a bool and reads more clearly. count() works in every C++ version.
Why is count() useful on a set if it is only 0 or 1? It is a short exists check, and the same function name works on containers where larger counts are possible, such as multiset.
Next: crbegin() Keep going through the std::set functions. Get a read-only reverse iterator to the biggest value (C++11).
crbegin() 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.
12 people found this page helpful
Helpful Share Copy link Suggestion