In C++, std::list::assign_range() replaces the content of a std::list with the elements of a range. A range is anything you can loop over: a vector, an array, a deque, 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 you do not write begin() and end(). 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 assign_range with one range. Compile with a C++23 compiler setting, for example -std=c++23.
C++
l.assign_range(v); // v is a vector
l.assign_range(std::views::iota(1, 6)); // 1 2 3 4 5
Call
What it does
l.assign_range(v)
Erases the old elements and copies every element of v into new nodes.
l.assign_range(std::views::iota(1, 6))
Fills the list 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 list.
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 <iostream>
#include <list>
#include <vector>
int main() {
std::vector<int> v = {3, 6, 9};
std::list<int> l = {1, 1, 1, 1, 1};
l.assign_range(v);
std::cout << "size: " << l.size() << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
size: 3
3 6 9
How It Works
The list begins with five nodes, each holding 1.
assign_range(v) takes the whole vector as one argument. No begin() or end() is needed.
The five old nodes are destroyed, 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 <iostream>
#include <list>
#include <ranges>
int main() {
std::list<int> l;
l.assign_range(std::views::iota(1, 6));
for (int n : l) {
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. Nothing is stored yet.
assign_range() walks through that view and links one new node for each number it produces.
The list was empty at the start, so the result is just the five generated numbers.
Example 3 — assign_range() with std::views::transform
Chain a view onto a vector so that every element is squared on the way into the list.
C++
#include <iostream>
#include <list>
#include <ranges>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::list<int> l = {-1};
l.assign_range(v | std::views::transform([](int x) { return x * x; }));
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 4 9 16
How It Works
v | std::views::transform(...) is a lazy view. It squares each number only when somebody reads it.
assign_range() reads the view one item at a time and stores the squared value in a new node.
The old -1 is gone and the vector v still holds 1 2 3 4, because the view never changes its source.
Frequently Asked Questions
It is a C++23 member function that replaces the content of a list with all elements of a range. It is the range version of assign(first, last).
C++23. Compile with a flag such as -std=c++23 and use a recent compiler. Older standards do not have this member.
A range is anything with a beginning and an end that you can loop over, such as a vector, an array, a deque, or a view like std::views::iota. In C++20 and C++23, ranges can be passed to functions as one object.
assign(first, last) needs two iterators. assign_range(rg) needs one range object, which is shorter and also works with views. Both erase the old elements first.
Yes. std::vector and std::deque also got assign_range() in C++23, and it works in the same way.