C++ std::set find() Function

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

Definition

In C++, std::set::find() searches for a value and returns an iterator to it. Through the iterator you can read *it.

If the value is not in the set, find() returns end(). Always compare with end() before you use the result. The search never adds a 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 and compare the result with end().

C++
auto it = s.find("Raj");
if (it != s.end()) {
    std::cout << *it;
}
CallWhat it does
s.find(value)Returns an iterator to value, or s.end() if it is not found.
cs.find(value)On a const set it returns a const_iterator. For a set every iterator is already read-only for the value.

Never write *s.find(v) without checking. When the value is missing, find() returns end(), and dereferencing end() is undefined behavior.

Library declarations
C++
iterator find(const key_type& value);
const_iterator find(const key_type& value) const;
template<class K> iterator find(const K& x);   // transparent comparator

Parameters

ParameterDescription
valueThe value to search for.

Return Value

Return ValueDescription
iteratorAn iterator to the matching value, or end() when none matches. For a const set it is a const_iterator.

C++ Examples

Example 1 — find() Returns an Iterator

Search for a value that exists and one that does not.

C++
#include <iostream>
#include <set>
#include <string>

int main() {
    std::set<std::string> names = {"Mia", "Raj", "Zoe"};

    auto it = names.find("Raj");
    if (it != names.end()) {
        std::cout << "found: " << *it << '\n';
    }

    if (names.find("Tom") == names.end()) {
        std::cout << "Tom was not found\n";
    }
    return 0;
}

How It Works

  1. find("Raj") searches the sorted values and returns an iterator to "Raj". It is not end(), so it is safe to use.
  2. *it reads the stored value "Raj". There is no separate key and value, because the value is its own key.
  3. find("Tom") finds nothing, so it returns names.end(). The comparison is true and the message prints. The set is not changed.

Example 2 — find() Then erase()

Find a value and remove it in one search.

C++
#include <iostream>
#include <set>
#include <string>

int main() {
    std::set<std::string> team = {"Ann", "Ben", "Cy"};

    auto it = team.find("Ben");
    if (it != team.end()) {
        team.erase(it);
    }

    for (const std::string& name : team) {
        std::cout << name << '\n';
    }
    return 0;
}

How It Works

  1. find("Ben") returns an iterator to that value. Checking against end() makes sure the erase is safe.
  2. erase(it) removes that one element. Using the iterator avoids a second search that erase("Ben") would do.
  3. The remaining values print in sorted order: Ann, then Cy.

Example 3 — find() in a Helper

Write a small function that returns true when a value is present, without changing the set.

C++
#include <iostream>
#include <set>
#include <string>

bool hasName(const std::set<std::string>& s, const std::string& name) {
    return s.find(name) != s.end();
}

int main() {
    std::set<std::string> guests = {"apple", "pear"};

    std::cout << std::boolalpha;
    std::cout << "apple: " << hasName(guests, "apple") << '\n';
    std::cout << "kiwi: " << hasName(guests, "kiwi") << '\n';
    std::cout << "size: " << guests.size() << '\n';
    return 0;
}

How It Works

  1. The function takes the set by const reference. find() works on a const set, and a set has no operator[] that could insert.
  2. For "apple", find() succeeds and the helper returns true. For "kiwi" it returns end(), so the helper gives false.
  3. The size is still 2, which proves that looking up a missing value with find() adds nothing.

Frequently Asked Questions

An iterator to the value, or end() if the value is not in the set.
Compare the result with s.end(). If it is not equal to end(), the value was found.
No. Elements in a set are const. To change one, erase the old value and insert the new one.
count() only says whether the value exists (0 or 1). find() gives an iterator so you can read or erase it without searching again.
It takes logarithmic time because a set is a balanced tree. For a million values, that is about 20 comparisons.

Next: get_allocator()

Keep going through the std::set functions. Get a copy of the allocator the set uses for its nodes.

get_allocator() 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.

13 people found this page helpful