In C++, std::set::insert()adds a value to the set and puts it in its sorted place. If the same value is already stored, insert() does nothing, because a set never keeps duplicates.
The single-value form returns a pair. The first part is an iterator to the value, and the second part is a bool that is true only when the value was new. Include <set> to use it.
This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.
Syntax
Pass one value, a hint and a value, a range, or a brace list.
C++
auto r = s.insert(5); // pair: iterator and bool
bool added = r.second; // false if 5 was already there
s.insert(s.end(), 9); // value with a position hint
s.insert(v.begin(), v.end()); // a range of values
s.insert({1, 2, 3}); // a brace list
Call
What it does
s.insert(value)
Adds value if it is new. Returns pair<iterator, bool>.
s.insert(hint, value)
Same, but the hint iterator helps the set find the place faster. Returns an iterator only.
s.insert(first, last)
Adds every value in the range. Repeats are skipped. Returns nothing.
s.insert({a, b, c})
Adds every value in the brace list. Returns nothing.
s.insert(std::move(node))
Adds a node handle taken from extract() (C++17).
If you only need to build the value in place, emplace() avoids a copy. For a bulk add from a whole range in C++23, there is insert_range(). A set has no operator[], so insert() is the main way to add values.
25 added: true, value 25
20 added: false, value 20
size: 4
How It Works
insert(25) is a new value, so it is stored in its sorted place between 20 and 30. The pair has second equal to true.
insert(20) finds 20 already stored. Nothing changes and second is false. The iterator first still points at the old 20, which is why *r2.first prints 20.
Only one of the two calls added a value, so the size is 4: 10, 20, 25, 30.
Example 2 — insert() with a Range
Add many values from a vector and from a brace list in two short calls.
s.insert(v.begin(), v.end()) reads the whole vector. The second 5 is a repeat, so only 5, 1, 3 are stored, in the sorted order 1, 3, 5.
s.insert({9, 1}) adds the brace list. 9 is new and goes at the end, but 1 is already stored and is skipped.
The loop prints 1 3 5 9. By coincidence both containers end with four values, but the set holds four different numbers, while the vector still has its repeat.
Example 3 — insert() to Detect Duplicates
Use the bool result to print every word that appears a second time.
C++
#include <iostream>
#include <set>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"to", "be", "or", "not", "to", "be"};
std::set<std::string> seen;
for (const std::string& w : words) {
if (!seen.insert(w).second) {
std::cout << "duplicate: " << w << '\n';
}
}
std::cout << "unique words: " << seen.size() << '\n';
return 0;
}
Output
duplicate: to
duplicate: be
unique words: 4
How It Works
For each word the loop calls seen.insert(w) and reads .second directly from the returned pair.
The first four words are new, so second is true and nothing is printed. The fifth word to and the sixth word be are already stored, so !second is true and they are reported.
The set keeps be, not, or, to, so the final count is 4. This one-line check is the classic way to find repeats without a second search.
Frequently Asked Questions
Nothing is added. The set is unchanged, and the second part of the returned pair is false.
The single-value form returns a pair of an iterator to the stored value and a bool that tells if it was new. The range and brace list forms return nothing.
Yes. Each new value goes into its sorted place, so a loop over the set always gives sorted values.
A set decides the position itself from the sort order, so you cannot choose the end. Use insert() or emplace().
Two values that are equal under the comparison are treated as the same value, even if some fields differ. With a custom comparator, the second one is ignored.