C++ std::set insert() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::set

Definition

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
CallWhat 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.

Library declarations
C++
std::pair<iterator, bool> insert(const value_type& value);
std::pair<iterator, bool> insert(value_type&& value);
iterator insert(const_iterator hint, const value_type& value);
template<class InputIt> void insert(InputIt first, InputIt last);
void insert(std::initializer_list<value_type> ilist);
insert_return_type insert(node_type&& nh);   // C++17

Parameters

ParameterDescription
valueThe value to add. It is copied or moved into the set.
hintAn iterator near where the value will go. It is only a hint and a wrong hint is still safe.
first, lastA range of values to add. last is one past the final value.
ilistA brace list such as {1, 2, 3}.

Return Value

Return ValueDescription
pair<iterator, bool>first points to the stored value. second is true if it was added and false if it was already there.
iteratorThe hint form returns an iterator to the stored value.
voidThe range and brace list forms return nothing.

C++ Examples

Example 1 — insert() Tells You If the Value Was New

Insert one new value and one repeated value, and print what the pair says.

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> s = {10, 20, 30};

    auto r1 = s.insert(25);
    auto r2 = s.insert(20);

    std::cout << "25 added: " << std::boolalpha << r1.second << ", value " << *r1.first << '\n';
    std::cout << "20 added: " << r2.second << ", value " << *r2.first << '\n';
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. 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.
  2. 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.
  3. 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.

C++
#include <iostream>
#include <set>
#include <vector>

int main() {
    std::vector<int> v = {5, 1, 5, 3};
    std::set<int> s;

    s.insert(v.begin(), v.end());
    s.insert({9, 1});

    std::cout << "values:";
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << '\n';
    std::cout << "vector size: " << v.size() << ", set size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. 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.
  2. s.insert({9, 1}) adds the brace list. 9 is new and goes at the end, but 1 is already stored and is skipped.
  3. 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;
}

How It Works

  1. For each word the loop calls seen.insert(w) and reads .second directly from the returned pair.
  2. 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.
  3. 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.

Next: insert_range()

Keep going through the std::set functions. Add every value of a range in one call (C++23).

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

15 people found this page helpful