C++ std::set count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::set

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.

C++
if (s.count("Raj") == 1) {
    // value exists
}
bool has = s.count("Zoe") > 0;
CallWhat 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
C++
size_type count(const key_type& value) const;
template<class K> size_type count(const K& x) const;   // transparent comparator

Parameters

ParameterDescription
valueThe value to look for.

Return Value

Return ValueDescription
1The value is in the set.
0The value is not in the set.

C++ Examples

Example 1 — count() Returns 0 or 1

Ask for one value that exists and one that does not.

C++
#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

  1. count("Raj") searches the sorted values and finds one match, so the result is 1.
  2. count("Zoe") finds none, so the result is 0. Because values are unique, it can never be 2 or more.
  3. 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.

C++
#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;
}

How It Works

  1. The loop reads each color. count(color) == 0 is true only when the color has never been stored.
  2. 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.
  3. 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.

C++
#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;
}

How It Works

  1. menu has three drinks. wanted lists four items, two of which are not on the menu.
  2. Each menu.count(item) is 1 for tea and juice, and 0 for cake and soup. Adding them gives 2.
  3. This only works as a sum because the answer is always 0 or 1 for a std::set.

Frequently Asked Questions

The number of times the value is stored. For a std::set that is only 0 or 1, because values are unique.
No. It only looks. A set has no operator[], so you never insert by accident with a lookup.
count() only says whether the value exists. find() returns an iterator, so you can also use or erase the value without a second search.
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.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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