W3cubDocs

/C++

std::vector<T,Allocator>::assign_range

template< container-compatible-range<T> R >
constexpr void assign_range( R&& rg );
(since C++23)

Replaces elements in the container with a copy of each element in rg. Invalidate all references, pointers, and iterators referring to the elements of the container, and the past-the-end iterator. Each iterator in the range rg is dereferenced exactly once.

The behavior is undefined if rg overlaps with the container.

Parameters

rg - an input_range with reference type convertible to the element type of the container
Type requirements
-std::assignable_from<T&, ranges::range_reference_t<R>> must be modeled. Otherwise, the program is ill-formed.
-T must be EmplaceConstructible into the container from *ranges::begin(rg). If R models neither sized_range nor forward_range, T must be also MoveInsertable into the container. Otherwise, the behavior is undefined.

Return value

(none).

Notes

Feature-test macro Value Std Comment
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware construction and insertion

Example

#include <algorithm>
#include <cassert>
#include <vector>
#include <list>
 
int main()
{
    const auto source = std::list{2, 7, 1};
    auto destination = std::vector{3, 1, 4};
    destination.assign_range(source);
    assert(std::ranges::equal(source, destination));
}

See also

(C++23)
inserts a range of elements
(public member function)
(C++23)
adds a range of elements to the end
(public member function)
assigns values to the container
(public member function)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/container/vector/assign_range