In C++, std::deque::insert_range() inserts every element of a range into a std::deque, before a position that you give as an iterator. A range is anything you can loop over: a vector, an array, a list, or a view.
It was added in C++23. It is a shorter way to write insert(pos, rg.begin(), rg.end()). Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax
Pass the position and one range. Compile with a C++23 compiler setting, for example -std=c++23.
C++
auto it = d.insert_range(d.begin() + 1, v);
d.insert_range(d.end(), lst);
Call
What it does
d.insert_range(pos, rg)
Copies every element of rg into the deque before pos.
d.insert_range(d.end(), rg)
Adds the range at the back of the deque.
In C++23 a deque also has append_range() (back) and prepend_range() (front) for the two ends.
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. The new elements are placed before it.
rg
Required. A range whose element type can be converted to T. It must not refer to this same deque.
Return Value
Return Value
Description
Iterator
An iterator to the first inserted element. If the range is empty, it is pos.
C++ Examples
Example 1 — insert_range() from a Vector
Put 2 3 4 between 1 and 5. Compile with -std=c++23.
C++
#include <deque>
#include <iostream>
#include <vector>
int main() {
std::deque<int> d = {1, 5};
std::vector<int> v = {2, 3, 4};
d.insert_range(d.begin() + 1, v);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 3 4 5
How It Works
d.begin() + 1 is the position of 5.
insert_range(..., v) adds all three values of the vector before that position. You pass the vector itself, not two iterators.
The result is 1 2 3 4 5, and the vector v stays unchanged.
Example 2 — insert_range() at the Front
Use the returned iterator to see where the new elements start.
C++
#include <array>
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {30, 40};
std::array<int, 2> a = {10, 20};
auto it = d.insert_range(d.begin(), a);
std::cout << "first inserted: " << *it << '\n';
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
first inserted: 10
10 20 30 40
How It Works
d.begin() as the position means the new elements go to the very front.
The returned iterator it points to the first inserted element, which is 10.
The old values 30 40 keep their order and now sit after the new ones.
Example 3 — insert_range() from a std::list
The source range can be any container, even one that is not contiguous.
C++
#include <deque>
#include <iostream>
#include <list>
int main() {
std::deque<int> d = {1, 2};
std::list<int> lst = {3, 4};
d.insert_range(d.end(), lst);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 3 4
How It Works
lst is a std::list. It is a valid range, because you can loop over it.
d.end() is the position, so the list elements are added at the back of the deque.
The values are copied in their original order, giving 1 2 3 4.
Frequently Asked Questions
It inserts all elements of a range into the deque, before a given position, and returns an iterator to the first inserted element.
C++23. With older compilers or older -std settings it is not available, so use insert(pos, first, last).
insert(pos, first, last) needs two iterators. insert_range(pos, rg) takes the whole range as one argument, which is shorter and also works with views.
No. The range must not refer to the same deque. Copy the part into another container first.
In C++23 use prepend_range(rg) for the front and append_range(rg) for the back.