C++ std::set Functions

About This Index
This page is the C++ std::set Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a std::set is on /cpp/set. Use this page when you need a specific member function such as insert(), find(), or lower_bound().
A std::set<T> stores values, keeps them in sorted order, and never allows a duplicate. The value is its own key, so there is no operator[] and no at(). Elements are const, so to change one you erase it and insert a new one. A set also has no capacity(), reserve(), or push_back(). The 29 functions are listed below.
How to Call a Set Function
Include <set>, create the set, and call members with a dot:
#include <iostream>
#include <set>
#include <string>
std::set<int> s = {30, 10, 20, 10}; // 10 20 30 (duplicate dropped)
s.insert(25); // 10 20 25 30
s.emplace(5); // 5 10 20 25 30
auto it = s.find(20); // iterator, or s.end()
bool has = s.contains(99); // false (C++20)
auto lb = s.lower_bound(22); // points to 25
s.erase(10); // 5 20 25 30
std::size_t n = s.size(); // 4 C++ std::set Functions Table
All function guides in this C++ tutorial series (29 tutorials), grouped by purpose.
Capacity
3 functionsAsk how many values a set holds. A set has no capacity(), reserve(), or resize().
Modifiers
9 functionsAdd, move, or remove values. The values stay unique and the set stays sorted after every change.
| Function | Description | Tutorial |
|---|---|---|
clear() | Remove every value so the set becomes empty. | Try it |
insert() | Add a value to the set. A repeated value is ignored. | Try it |
insert_range() | Add every value of a range in one call (C++23). | Try it |
emplace() | Build a value directly inside the set (C++11). | Try it |
emplace_hint() | Build a value in place, using a hint for its position (C++11). | Try it |
erase() | Remove a value, one position, or a range from the set. | Try it |
extract() | Take a value out of the set as a node you can edit or move (C++17). | Try it |
merge() | Move all new values from another set into this one (C++17). | Try it |
swap() | Swap all values with another set of the same type. | Try it |
Lookup
6 functionsSearch for a value in logarithmic time. Because the values are sorted, you can also find the first value at or after a limit.
| Function | Description | Tutorial |
|---|---|---|
count() | Tell whether a value exists: the answer is 0 or 1 for a set. | Try it |
find() | Search for a value and get an iterator to it, or end(). | Try it |
contains() | Return true if the value is in the set (C++20). | Try it |
equal_range() | Get the range of values equal to the given value. | Try it |
lower_bound() | Find the first value that is not less than the given value. | Try it |
upper_bound() | Find the first value that is greater than the given value. | Try it |
Observers
3 functionsGet the helper objects that a set uses to compare values and to allocate memory.
| Function | Description | Tutorial |
|---|---|---|
key_comp() | Get the function object that compares two values. | Try it |
value_comp() | Get the function object that compares two set elements. | Try it |
get_allocator() | Get a copy of the allocator the set uses for its nodes. | Try it |
Iterators
8 functionsWalk through the values in sorted order, forwards or backwards. The c versions are read-only, but every set iterator is already read-only.
| Function | Description | Tutorial |
|---|---|---|
begin() | Get an iterator to the smallest value. | Try it |
end() | Get the iterator just past the last value. | Try it |
cbegin() | Get a read-only iterator to the first value (C++11). | Try it |
cend() | Get a read-only iterator past the last value (C++11). | Try it |
rbegin() | Get a reverse iterator to the biggest value. | Try it |
rend() | Get the reverse iterator just before the first value. | Try it |
crbegin() | Get a read-only reverse iterator to the biggest value (C++11). | Try it |
crend() | Get a read-only reverse iterator before the first value (C++11). | Try it |
Frequently Asked Questions
Start with insert()
Add a value to a set. Duplicates are ignored, so it is a good first step.
12 people found this page helpful
