In C++, std::list::insert_range() inserts every element of a range into a std::list, right before the position you give. The range can be a vector, an array, a deque, or a lazy view.
It was added in C++23. It is the range version of insert(pos, first, last): you pass one range instead of two iterators. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call insert_range with an iterator and one range. Compile with C++23, for example -std=c++23.
C++
auto it = l.insert_range(pos, v); // v is a vector
l.insert_range(l.end(), std::views::iota(3, 6)); // adds 3 4 5
Call
What it does
l.insert_range(pos, rg)
Inserts every element of rg before pos and returns an iterator to the first new element.
l.insert_range(l.end(), rg)
Adds the range at the very end of the list.
If your compiler is older than C++23, use insert(pos, first, last). For adding at the end only, C++23 also offers append_range().
Library declarations
C++
template<container-compatible-range<T> R>
iterator insert_range(const_iterator pos, R&& rg); // since C++23
Parameters
Parameter
Description
pos
Required. An iterator into this list. The new elements go before it. l.end() is allowed.
rg
Required. A range whose elements can be converted to T. It must not refer to this same list.
Return Value
Return Value
Description
iterator
An iterator to the first inserted element. If the range is empty, it returns pos.
C++ Examples
Example 1 — insert_range() from a Vector
Insert two vector values between two list values. Compile with -std=c++23.
C++
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
int main() {
std::vector<int> v = {20, 30};
std::list<int> l = {10, 40};
l.insert_range(std::next(l.begin()), v);
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
10 20 30 40
How It Works
std::next(l.begin()) is an iterator to 40, the second node.
insert_range() links one new node for 20 and one for 30 before that position.
The vector v is unchanged. Its values were copied into the list.
Example 2 — insert_range() Returns the First New Element
Add generated numbers at the end and use the returned iterator to find where they start.
C++
#include <iostream>
#include <list>
#include <ranges>
int main() {
std::list<int> l = {1, 2};
auto it = l.insert_range(l.end(), std::views::iota(3, 6));
std::cout << "first new: " << *it << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
first new: 3
1 2 3 4 5
How It Works
std::views::iota(3, 6) produces 3 4 5 without storing them anywhere.
Passing l.end() as the position adds the new nodes after the last element.
The returned iterator points at the first new node, so *it is 3, not the last element.
Example 3 — insert_range() with an Empty Range
When the range has no elements, nothing changes and the position comes back.
The vector none has zero elements, so there is nothing to copy.
When the range is empty, insert_range() returns the pos that you passed, here l.begin().
The list still has two elements, 8 and 9. An empty insert is harmless.
Frequently Asked Questions
It is a C++23 member function that inserts all elements of a range before a given iterator. It is the range version of insert(pos, first, last).
C++23. Use a flag such as -std=c++23 and a compiler with C++23 library support. Older standards do not have it.
An iterator to the first inserted element, or pos if the range was empty.
insert(pos, first, last) takes two iterators. insert_range(pos, rg) takes one range object, which is shorter and also works with views such as std::views::iota.
No. insert(pos, first, last) does the same job and works in every C++ version. insert_range() just gives a cleaner call when you already have a range.