Home C++ Maps Functions insert_range() C++ std::map insert_range() Function 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.
std::vector<std::pair<std::string, int>> v = {{"a", 1}, {"b", 2}};
m.insert_range(v); // C++23 Call What 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 template<container-compatible-range<value_type> R>
void insert_range(R&& rg); // C++23 Parameters Parameter Description rgA range (vector, array, list, another map, ...) whose elements can be turned into std::pair<const Key, T>.
Return Value Return Value Description voidNothing is returned. Check size() to see how many pairs were added.
Important
insert_range() is new in C++23 . Older compilers do not have it. If the range holds repeated keys , the first one wins and later ones are skipped, exactly like insert(). It never overwrites existing values.
C++ Examples 1. Insert from a vector 2. Insert from a C array 3. Same result as insert(first, last) Example 1 — insert_range() with a Vector Put all pairs of a vector into a map. A repeated key is skipped.
#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 The vector holds three pairs, and the key "b" appears twice, with 2 and then 9. 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.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.
#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 A plain array is a range, so it can be passed straight to insert_range() without calling begin() and end(). The map already holds key 2 with "TWO". The array pair {2, "two"} is skipped, so the old value stays. 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.
#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;
} same size: true
same content: true How It Works Both maps start empty and receive the same three pairs from the same vector. a uses the old iterator-pair form. b uses insert_range(v), which only saves you from typing begin() and end().Two maps compare equal with == when they hold the same pairs in the same order, so both lines print true. Frequently Asked Questions What does std::map::insert_range() do? 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.
Which C++ version has insert_range()? C++23. If your compiler does not support it, use insert(first, last), which has been available since C++98.
Does insert_range() overwrite existing values? No. Like insert(), it keeps the old value when the key already exists. To update, use insert_or_assign() in a loop.
What if the range has the same key twice? The first pair with that key is inserted and the later ones are skipped.
What can I pass as the range? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion