C++ std::set insert_range() Function

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

Definition

In C++23, std::set::insert_range() adds every value of a range to the set. A range can be a std::vector, a std::array, a string, or a view. You pass the whole object, not two iterators.

Each value is inserted as if by insert(), so repeats are skipped and the set stays sorted. The function needs a C++23 compiler and <set>.

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 any range. Compile with -std=c++23.

C++
std::vector<int> v = {7, 3, 7, 5};
std::set<int> s;
s.insert_range(v);          // 3 5 7
CallWhat it does
s.insert_range(range)Inserts every value of range. Values already in the set are skipped. Returns nothing.

Before C++23 you wrote the same thing as s.insert(v.begin(), v.end()). Both do the same work. insert_range() is shorter and works with range views too. If your compiler does not support C++23 yet, use the two-iterator insert().

Library declarations
C++
template<container-compatible-range<value_type> R>
void insert_range(R&& rg);   // C++23

Parameters

ParameterDescription
rgA range whose values can be converted to the value type of the set. A std::vector or std::array works.

Return Value

Return ValueDescription
voidNothing is returned. Use size() to see how many values the set holds now.

C++ Examples

Example 1 — insert_range() Adds a Whole Vector

Put every number of a vector into a set. Repeats are dropped.

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

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

    s.insert_range(v);

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

How It Works

  1. s.insert_range(v) takes the whole vector. You do not write begin() and end().
  2. The vector has four values, but 7 appears twice. The second 7 is skipped, so only three values are stored.
  3. The loop shows the values in sorted order, 3, 5, 7, even though the vector was not sorted. The size is 3.

Example 2 — insert_range() Keeps Old Values

Add a range to a set that is not empty and see how many values were really new.

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

int main() {
    std::set<int> s = {1, 2};
    std::vector<int> more = {2, 3, 4};

    std::size_t before = s.size();
    s.insert_range(more);
    std::size_t added = s.size() - before;

    std::cout << "added " << added << " new values\n";
    std::cout << "now:";
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The set starts with 1, 2 and the range has 2, 3, 4. The value 2 is in both.
  2. The call does not return a count, so the program saves size() before and subtracts it after. Only 3 and 4 were new, so the answer is 2.
  3. The set now holds 1 2 3 4. The old values stay in place and the new ones go into their sorted positions.

Example 3 — insert_range() with a String

A string is a range of characters, so it can go straight into a set of char.

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

int main() {
    std::string word = "hello";
    std::set<char> letters;

    letters.insert_range(word);

    std::cout << "letters:";
    for (char c : letters) {
        std::cout << ' ' << c;
    }
    std::cout << '\n';
    std::cout << word.size() << " characters, " << letters.size() << " different\n";
    return 0;
}

How It Works

  1. A std::string is a range of char values, so insert_range(word) works with no extra code.
  2. The word hello has two letter l characters. The set stores one, so the letters are e, h, l, o in alphabetical order.
  3. The string has 5 characters while the set has 4 different ones. The original string is not changed.

Frequently Asked Questions

It was added in C++23. Compile with a flag such as -std=c++23 and use a library that supports it.
insert(first, last) takes two iterators. insert_range(range) takes the whole range object, and it also works with range views. The result in the set is the same.
No. Values that are already in the set, or repeated in the range, are skipped. The set keeps one copy of each.
No, it returns void. Compare size() before and after the call if you need the count.
Any range whose elements convert to the set value type, such as a vector, an array, a string, or another set.

Next: key_comp()

Keep going through the std::set functions. Get the function object that compares two values.

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

7 people found this page helpful