In C++, std::set<T> is a container that stores unique values in sorted order. If you add a value that is already inside, the set quietly ignores it. Think of it as a guest list where every name appears once and the names are always in alphabetical order.
Inside, a set is a balanced tree, so finding, inserting, and erasing a value all take logarithmic time. Check for a value with find() or contains(), and add values with insert() or emplace(). Include <set> to use it.
This page teaches what a std::set is. The Set Functions index lists every function guide in this series.
Why choose a set?
Choose a set when you only care whether a value is present, want no duplicates, and want the values sorted, for example unique user names, visited ids, or a sorted list of distinct scores. A set has no operator[] and no at(), because the value is its own key. If you also need a value attached to each key, use a std::map.
Syntax
C++ Syntax
Common ways to create and use a std::set:
C++
#include <iostream>
#include <set>
#include <string>
std::set<int> a; // empty set
std::set<int> b = {30, 10, 20, 10}; // sorted: 10 20 30
std::set<int, std::greater<int>> c = {30, 10, 20}; // 30 20 10, biggest first
std::set<std::string> d = {"pear", "apple"};
b.insert(25); // add only if the value is new
b.emplace(5); // build the value in place
bool has = b.contains(20); // true (C++20)
auto it = b.find(30); // iterator, or b.end()
b.erase(10); // remove by value
std::size_t n = b.size(); // number of values
Reference
std::set vs std::unordered_set vs std::vector
Here is a quick comparison of containers that beginners often mix up. A std::map and a std::multiset are included too:
Feature
std::set
std::unordered_set
std::multiset
std::map
std::vector
Stores
Values
Values
Values
Key and value pairs
Values
Order
Sorted
No order
Sorted
Sorted by key
The order you added them
Duplicates
Not allowed
Not allowed
Allowed
Keys not allowed twice
Allowed
Find a value
Logarithmic
Usually constant
Logarithmic
Logarithmic
Linear
Ordered search (lower_bound())
Yes
No
Yes
Yes
Only if sorted
Change an element in place
No, it is const
No, it is const
No, it is const
Value yes, key no
Yes
Header
<set>
<unordered_set>
<set>
<map>
<vector>
Guide
When to Use insert(), find() or contains()
A set has no s[i] and no at(). You work with it through these functions. Pick the one that fits:
unique scores: 60 70 85 92
first score from 80: 85
7 scores, 4 different
How It Works
The set is built from the whole vector. Every repeat of 70, 85, and 92 is skipped, and the four different values are sorted.
The loop prints 60 70 85 92. The original vector is not changed, so it still has seven values.
lower_bound(80) returns the first value that is not smaller than 80. 60 and 70 are smaller, so it lands on 85. The check against end() keeps the read safe.
Continue
Next: C++ std::set Functions Index
After you know what a std::set is, open the function index for a full tutorial on each member function.
std::set<T> is a container from <set>. It stores unique values, keeps them in sorted order, and finds, inserts, or erases a value in logarithmic time.
A set keeps values in sorted order and uses a balanced tree, so find is logarithmic. An unordered_set uses hashing, has no order, and is usually faster to find a value. Choose set when you need sorted output or range searches such as lower_bound().
A set allows each value once and ignores a repeat. A multiset is also sorted but allows duplicates, so count() can be larger than 1.
No. The elements are const, because changing a value could break the sorted order. To change one, erase the old value and insert the new one, or use extract() in C++17 to move the value out and edit it.
Open the C++ std::set Functions index. It lists every member function, from insert() and find() to lower_bound() and extract(), each with its own tutorial.