Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::set — an ordered collection of unique keys

std::set from <set> stores unique keys in comparator order. Learn insertion, lookup, and the differences between find, contains, count, and equal_range, including how to check end() safely.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <set>
std::set<Key> values;
auto [it, inserted] = values.insert(key);
auto found = values.find(key);
bool present = found != values.end();
bool exists = values.contains(key);
auto occurrences = values.count(key);
auto [first, last] = values.equal_range(key);

std::set<Key> from <set> is an ordered associative container. Each element is itself a key; the default comparator std::less<Key> orders integers increasingly. Uniqueness means comparator equivalence: !comp(a, b) && !comp(b, a), not necessarily a == b. The comparator must establish a strict weak ordering.

The ordinary single-value overload of std::set::insert(key) returns a pair: an iterator to the inserted or existing element and a bool indicating whether insertion occurred. An equivalent key is not added again. std::set::find(key) returns an iterator to an equivalent element or std::set::end(). end() points past the last element: check it != values.end() before dereferencing a lookup result.

std::set::contains(key) returns a Boolean membership result. std::set::count(key) returns the number of equivalent elements, always 0 or 1 here. std::set::equal_range(key) returns two iterators describing the half-open range [first, last) of equivalent elements: empty or containing one element. An empty range means first == last, but not necessarily first == values.end().

Ordinary single-value insertion and the listed lookup operations have O(log n) complexity; end() takes constant time. Keys cannot be modified through iterators. The example targets compilation with -std=c++23. The supplied document is an evolving draft, not a frozen C++23 publication; the repository compiler flag does not attest production runtime support.

Examples

#include <iostream>
#include <set>

int main() {
    std::set<int> values{3, 1, 3};
    auto [it, inserted] = values.insert(2);
    auto duplicate = values.insert(3);

    std::cout << std::boolalpha;
    std::cout << "insert: " << *it << ' ' << inserted << '\n';
    std::cout << "duplicate: " << duplicate.second << '\n';
    std::cout << "ordered:";
    for (int value : values) {
        std::cout << ' ' << value;
    }
    std::cout << '\n';

    auto found = values.find(2);
    if (found != values.end()) {
        std::cout << "find: " << *found << '\n';
    }
    std::cout << "contains 4: " << values.contains(4) << '\n';
    std::cout << "count 3: " << values.count(3) << '\n';

    auto [first, last] = values.equal_range(2);
    std::cout << "equal_range 2:";
    for (auto current = first; current != last; ++current) {
        std::cout << ' ' << *current;
    }
    std::cout << '\n';
}

Result

insert: 2 true
duplicate: false
ordered: 1 2 3
find: 2
contains 4: false
count 3: 1
equal_range 2: 2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources