C++ Sets

A C++ set is an STL container that stores unique values in sorted order. Use std::set when each value should appear only once and you want C++ to keep the values ordered for you.

Sets are different from vectors and lists because they are not mainly about position. They are mainly about membership: whether a value is present or not.

Including the Set Header

To use a set, include the <set> header. A set type is written as std::set<T>, where T is the type of value stored in the set.

#include <iostream>
#include <set>
#include <string>

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

    names.insert("Ben");
    names.insert("Alex");

    for (std::string name : names) {
        std::cout << name << std::endl;
    }

    return 0;
}

Output:

Alex
Ben
Mia
Zoe

The duplicate values are ignored. "Mia" appears only once even though it was listed twice, and "Alex" is not added a second time. The output is alphabetical because std::set stores its elements in sorted order.

Adding Values

The insert() function adds a value to a set. If the value is already present, the set stays the same because every element must be unique.

Unlike a vector, a set does not support index access such as items[0]. The first element is not necessarily the first value you inserted; it is the smallest value according to the set’s ordering.

Finding and Removing Values

The find() function searches for a value. If it finds the value, it returns an iterator pointing to that element. If it does not find the value, it returns end(). The erase() function removes a value if it exists.

#include <iostream>
#include <set>

int main(void) {
    std::set<int> scores = {80, 95, 70, 88};

    scores.erase(70);
    scores.insert(100);

    if (scores.find(95) != scores.end()) {
        std::cout << "95 is in the set" << std::endl;
    }

    std::cout << "Has 70: " << scores.count(70) << std::endl;
    std::cout << "Size: " << scores.size() << std::endl;

    for (int score : scores) {
        std::cout << score << std::endl;
    }

    return 0;
}

Output:

95 is in the set
Has 70: 0
Size: 4
80
88
95
100

The value 70 is removed, and 100 is added. Because sets are sorted, the final loop prints the scores from lowest to highest.

Using count()

The count() function tells you how many elements match a value. In a std::set, the answer is always 0 or 1, because duplicates are not allowed.

Use count(value) > 0 when you only need a yes-or-no membership test. Use find(value) when you need an iterator to the found element.

Common Set Functions

Function What it does
insert(value) Adds a value if it is not already present
erase(value) Removes a matching value
find(value) Returns an iterator to the value or end()
count(value) Returns 1 if present, otherwise 0
size() Returns the number of stored elements
empty() Returns whether the set has no elements

When To Use a Set

Use std::set when you need unique values, sorted iteration, and fast lookup by value. Use a vector when duplicates and index positions matter more than membership tests.

Takeaway: std::set stores one copy of each value and keeps those values sorted automatically.