C++ std::unordered_set insert_range() Function

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

Definition

In C++23, std::unordered_set::insert_range() adds every value of a range to the unordered_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 unordered_set stays sorted. The function needs a C++23 compiler and <unordered_set>.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Pass any range. Compile with -std=c++23.

C++
std::vector<int> v = {7, 3, 7, 5};
std::unordered_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 unordered_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 unordered_set. A std::vector or std::array works.

Return Value

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

C++ Examples

Example 1 — insert_range() Adds a Whole Vector

Put every number of a vector into an unordered_set. Repeats are dropped.

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

int main() {
    std::vector<int> v = {7, 3, 7, 5};
    std::unordered_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 an unspecified 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 an unordered_set that is not empty and see how many values were really new.

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

int main() {
    std::unordered_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 unordered_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 unordered_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 an unordered_set of char.

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

int main() {
    std::string word = "hello";
    std::unordered_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 unordered_set stores one, so the letters are e, h, l, o in an unspecified order.
  3. The string has 5 characters while the unordered_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 unordered_set is the same.
No. Values that are already in the unordered_set, or repeated in the range, are skipped. The unordered_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 unordered_set value type, such as a vector, an array, a string, or another set.

Next: key_eq()

Keep going through the std::unordered_set functions. Get the value equality predicate the unordered_set uses.

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

9 people found this page helpful