C++ std::set Functions

Beginner
8 min read
Updated: Nov 2026
29 function guides
Function index

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 sorted container of unique values.

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:

C++
#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 functions

Ask how many values a set holds. A set has no capacity(), reserve(), or resize().

FunctionDescriptionTutorial
empty()Check whether a set has no values at all.Try it
size()Count how many unique values are in the set.Try it
max_size()Get the largest number of values a set could ever hold.Try it

Modifiers

9 functions

Add, move, or remove values. The values stay unique and the set stays sorted after every change.

FunctionDescriptionTutorial
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 functions

Search for a value in logarithmic time. Because the values are sorted, you can also find the first value at or after a limit.

FunctionDescriptionTutorial
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 functions

Get the helper objects that a set uses to compare values and to allocate memory.

FunctionDescriptionTutorial
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 functions

Walk through the values in sorted order, forwards or backwards. The c versions are read-only, but every set iterator is already read-only.

FunctionDescriptionTutorial
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

They are the functions you call on a std::set object with a dot, such as s.insert(5), s.find(5), s.erase(5), or s.size(). They need #include <set>.
This tutorial series covers 29: capacity (empty, size, max_size), modifiers (clear, insert, insert_range, emplace, emplace_hint, erase, extract, merge, swap), lookup (count, find, contains, equal_range, lower_bound, upper_bound), observers (key_comp, value_comp, get_allocator), and iterators (begin, end, cbegin, cend, rbegin, rend, crbegin, crend).
A set stores only values, and the value is its own key. There is nothing to look up by key and return, so there is no s[key] and no at(). To check a value, use find(), count(), or contains().
emplace(), emplace_hint(), and the c-iterators are C++11. extract() and merge() are C++17. contains() is C++20. insert_range() is C++23.
Read the C++ std::set page first. Then open insert(), find(), contains(), and erase() from the tables below.

Start with insert()

Add a value to a set. Duplicates are ignored, so it is a good first step.

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

12 people found this page helpful