Namespaces
Variants

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

From cppreference.net

template < container-compatible-range < T > R >
constexpr void assign_range ( R && rg ) ;
(начиная с C++23)

Заменяет элементы в контейнере копией каждого элемента из rg .

Все итераторы (включая end() итератор) и все ссылки на элементы становятся недействительными.

Каждый итератор в диапазоне rg разыменовывается ровно один раз.

Если rg перекрывается с * this , поведение не определено.

Содержание

Параметры

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 T is not EmplaceConstructible into vector from * ranges:: begin ( rg ) , the behavior is undefined.
-
If any of the following conditions is satisfied, and T is not MoveInsertable into vector , the behavior is undefined:
(until C++26)
(since C++26)

Примечания

Макрос тестирования возможностей Значение Стандарт Возможность
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware конструирование и вставка

Пример

#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));
}

Смотрите также

вставляет диапазон элементов
(публичная функция-член)
добавляет диапазон элементов в конец
(публичная функция-член)
присваивает значения контейнеру
(публичная функция-член)
присваивает значения контейнеру
(публичная функция-член)