In C++, std::deque::assign_range() replaces the content of a std::deque with the elements of a range. A range is anything you can loop over: a vector, an array, a list, or a lazy view such as std::views::iota.
It was added in C++23. Unlike assign(first, last), you pass the whole range as one argument, so there is no need to write begin() and 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
Call assign_range with one range. Compile with a C++23 compiler setting, for example -std=c++23.
C++
d.assign_range(v); // v is a vector
d.assign_range(std::views::iota(1, 6)); // 1 2 3 4 5
Call
What it does
d.assign_range(v)
Removes the old elements and copies every element of v into the deque.
d.assign_range(std::views::iota(1, 6))
Fills the deque from a view that produces 1 2 3 4 5.
If your compiler is older than C++23, use assign(first, last) instead. It does the same job.
Library declarations
C++
template<container-compatible-range<T> R>
void assign_range(R&& rg); // since C++23
Parameters
Parameter
Description
rg
Required. A range whose element type can be converted to T. It must not be a range that refers to this same deque.
Return Value
Return Value
Description
Nothing
assign_range() returns void. The old elements are replaced by copies of the range elements.
C++ Examples
Example 1 — assign_range() from a Vector
Replace five old values with the three values of a vector. Compile with -std=c++23.
C++
#include <deque>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 6, 9};
std::deque<int> d = {1, 1, 1, 1, 1};
d.assign_range(v);
std::cout << "size: " << d.size() << '\n';
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
size: 3
3 6 9
How It Works
The deque begins with five copies of 1.
assign_range(v) takes the whole vector as one argument. No begin() or end() is needed.
The old five elements are removed, so the size becomes 3, the size of the vector.
Example 2 — assign_range() with std::views::iota
A range does not need to be stored anywhere. A view can create numbers on demand.
C++
#include <deque>
#include <iostream>
#include <ranges>
int main() {
std::deque<int> d;
d.assign_range(std::views::iota(1, 6));
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 3 4 5
How It Works
std::views::iota(1, 6) describes the numbers from 1 up to, but not including, 6.
assign_range() walks through that view and stores each number in the deque.
The deque was empty at the start, so the result is just the five generated numbers.
Example 3 — assign_range() from a std::array
Show that the old content is really gone after the call.
After assign_range(src) the deque holds 8 7 6, in the same order as the array.
front() and back() show the first and last copied values. 100 and 200 do not appear anywhere.
Frequently Asked Questions
It replaces the content of the deque with the elements of a range. A range can be a vector, an array, a list, or a view such as std::views::iota.
C++23. With older compilers or older -std settings it does not exist, so use assign(first, last) instead.
assign(first, last) needs two iterators. assign_range(rg) needs one range object, which is shorter and works with views. Both remove the old elements first.
No. It destroys every old element and then stores copies of the elements from the range.
In C++23 use append_range(rg) to add at the back, prepend_range(rg) to add at the front, or insert_range(pos, rg) for any place. In older versions use insert(pos, first, last).