In C++, std::vector::assign_range() replaces the content of a std::vector 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 <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax
Call assign_range with one range. Compile with a C++23 compiler setting, for example -std=c++23.
C++
v.assign_range(v); // v is a vector
v.assign_range(std::views::iota(1, 6)); // 1 2 3 4 5
Call
What it does
v.assign_range(v)
Removes the old elements and copies every element of v into the vector.
v.assign_range(std::views::iota(1, 6))
Fills the vector 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 vector.
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 <vector>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 6, 9};
std::vector<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 vector 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 <vector>
#include <iostream>
#include <ranges>
int main() {
std::vector<int> v;
v.assign_range(std::views::iota(1, 6));
for (int n : v) {
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 vector.
The vector 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 vector 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 vector 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).