C++ std::set contains() Function

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

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.

C++
if (s.contains("Raj")) {
    // the value exists
}
bool has = s.contains("Zoe");
CallWhat 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
C++
bool contains(const key_type& value) const;   // C++20
template<class K> bool contains(const K& x) const;

Parameters

ParameterDescription
valueThe value to look for.

Return Value

Return ValueDescription
trueThe set contains this value.
falseThe set does not contain this value.

C++ Examples

Example 1 — contains() Returns a Bool

Print the result of contains() for a present and a missing value.

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

  1. boolalpha makes the program print true and false instead of 1 and 0.
  2. contains("Raj") finds the value and returns true.
  3. 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.

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

How It Works

  1. The loop tries two names: "ann" and "cy". Each is checked against the set.
  2. "ann" is stored, so contains() is true and the registered message prints.
  3. "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.

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

How It Works

  1. For each value, three tests are run: contains() (C++20), count() > 0, and find() != end().
  2. Value 2 is in the set, so all three say true.
  3. 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

It returns true if the set holds the given value and false if it does not.
C++20. In older versions use count(value) != 0 or find(value) != end().
No. It only checks. A set never inserts during a lookup.
It works, but it searches twice. If you need the iterator, call find() once.
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

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.

11 people found this page helpful