C++ std::unordered_set contains() Function

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

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.

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 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
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 unordered_set contains this value.
falseThe unordered_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 <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

  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 unordered_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 <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;
}

How It Works

  1. The loop tries two names: "ann" and "cy". Each is checked against the unordered_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 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.

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

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 unordered_set, so all three say true.
  3. 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

It returns true if the unordered_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. An unordered_set never inserts during a lookup.
It works, but it searches twice. If you need the iterator, call find() once.
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

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.

14 people found this page helpful