C++ std::map insert_range() Function

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

Definition

In C++23, std::map::insert_range() takes a whole range, such as a std::vector, an array, or another container of pairs, and inserts every pair into the map.

It is the range version of insert(first, last), but you pass the container itself. As with insert(), a pair whose key is already in the map is skipped and the old value stays. You need a C++23 compiler and -std=c++23.

This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Pass a range whose elements can be converted to the map's pair type.

C++
std::vector<std::pair<std::string, int>> v = {{"a", 1}, {"b", 2}};
m.insert_range(v);          // C++23
CallWhat it does
m.insert_range(range)Inserts each element of range. Elements with an existing key are skipped. Returns void.

If your compiler is older than C++23, use m.insert(v.begin(), v.end()). It gives the same result for this job.

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

Parameters

ParameterDescription
rgA range (vector, array, list, another map, ...) whose elements can be turned into std::pair<const Key, T>.

Return Value

Return ValueDescription
voidNothing is returned. Check size() to see how many pairs were added.

C++ Examples

Example 1 — insert_range() with a Vector

Put all pairs of a vector into a map. A repeated key is skipped.

C++
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

int main() {
    std::vector<std::pair<std::string, int>> v = {{"b", 2}, {"a", 1}, {"b", 9}};
    std::map<std::string, int> m;

    m.insert_range(v);   // C++23

    for (const auto& kv : m) {
        std::cout << kv.first << "=" << kv.second << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The vector holds three pairs, and the key "b" appears twice, with 2 and then 9.
  2. insert_range(v) walks the vector from the start. It inserts b=2 and a=1. When it reaches b=9, the key exists, so that pair is skipped.
  3. The map is sorted by key, so the loop prints a=1 b=2.

Example 2 — insert_range() with an Array

Any range works, including a plain C array of pairs.

C++
#include <iostream>
#include <map>
#include <string>
#include <utility>

int main() {
    std::pair<int, std::string> items[] = {{3, "three"}, {1, "one"}, {2, "two"}};
    std::map<int, std::string> m = {{2, "TWO"}};

    m.insert_range(items);   // C++23

    for (const auto& kv : m) {
        std::cout << kv.first << " " << kv.second << '\n';
    }
    return 0;
}

How It Works

  1. A plain array is a range, so it can be passed straight to insert_range() without calling begin() and end().
  2. The map already holds key 2 with "TWO". The array pair {2, "two"} is skipped, so the old value stays.
  3. Keys 3 and 1 are new and are added. The output is in key order: 1, 2, 3.

Example 3 — insert_range() Compared with the Iterator Pair

Build two maps, one with each style, and check that they are equal.

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

int main() {
    std::vector<std::pair<int, int>> v = {{5, 50}, {1, 10}, {3, 30}};

    std::map<int, int> a;
    std::map<int, int> b;

    a.insert(v.begin(), v.end());   // works in every C++ version
    b.insert_range(v);              // C++23

    std::cout << std::boolalpha;
    std::cout << "same size: " << (a.size() == b.size()) << '\n';
    std::cout << "same content: " << (a == b) << '\n';
    return 0;
}

How It Works

  1. Both maps start empty and receive the same three pairs from the same vector.
  2. a uses the old iterator-pair form. b uses insert_range(v), which only saves you from typing begin() and end().
  3. Two maps compare equal with == when they hold the same pairs in the same order, so both lines print true.

Frequently Asked Questions

It inserts all the pairs of a range, such as a vector or an array, into the map. Pairs whose keys already exist are skipped.
C++23. If your compiler does not support it, use insert(first, last), which has been available since C++98.
No. Like insert(), it keeps the old value when the key already exists. To update, use insert_or_assign() in a loop.
The first pair with that key is inserted and the later ones are skipped.
Any range whose elements can be converted to std::pair<const Key, T>, for example a vector of pairs, a C array of pairs, or another map.

Next: key_comp()

Keep going through the std::map functions. Get the function object that compares two keys.

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.

9 people found this page helpful