std::vector<T,Allocator>::assign_range
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Containers library
(C++17)
(C++11)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++23)
(C++23)
(C++20)
(C++23)
Tables
std::vector
Non-member functions
Deduction guides (C++17)
vector::assign_range
(C++23)
(C++11)
(C++11)
(C++11)
(C++11)
(DR*)
(C++23)
(C++23)
(C++11)
(C++11)
(C++20)
(C++20)(C++20)
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
template< container-compatible-range <T> R >
constexpr void assign_range( R&& rg );
(since C++23)
constexpr void assign_range( R&& rg );
Replaces elements in the container with a copy of each element in rg.
All iterators (including the end()
iterator) and all references to the elements are invalidated.
Each iterator in the range rg is dereferenced exactly once.
If rg overlaps with *this, the behavior is undefined.
Contents
[edit] Parameters
rg
-
an
input_range
with reference type convertible to the element type of the container
Type requirements
-If std::assignable_from <T&, ranges::range_reference_t <R>> is not modeled, the program is ill-formed.
-If any of the following conditions is satisfied, and
T
is not MoveInsertable into vector
, the behavior is undefined:
-
R
models neithersized_range
norforward_range
.
-
R
models approximately_sized_range but notsized_range
. -
R
modelsinput_range
but notforward_range
.
Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | Ranges-aware construction and insertion |
[edit] Example
Run this code
#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}; #ifdef __cpp_lib_containers_ranges destination.assign_range(source); #else destination.assign(source.cbegin(), source.cend()); #endif assert (std::ranges::equal (source, destination)); }