std::vector<T,Allocator>::assign_range
来自 cppreference.cn
template< container-compatible-range<T> R > constexpr void assign_range( R&& rg ); |
(自 C++23 起) | |
用 rg 中每个元素的副本替换容器中的元素。
所有迭代器(包括 end()
迭代器)和对元素的所有引用都将失效。
范围 rg 中的每个迭代器都正好被解引用一次。
如果 rg 与容器重叠,则行为未定义。
目录 |
[编辑] 参数
rg | - | 一个 input_range ,其引用类型可转换为容器的元素类型 |
类型要求 | ||
-std::assignable_from<T&, ranges::range_reference_t<R>> 必须被建模。否则,程序是非良构的。 | ||
-T 必须是从 *ranges::begin(rg) 就地构造 (EmplaceConstructible) 到容器中的。如果 R 既不建模 sized_range 也不建模 forward_range ,则 T 也必须是 可移动插入 (MoveInsertable) 到容器中的。否则,行为是未定义的。 |
[编辑] 返回值
(无)
注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__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)); }
[编辑] 参见
(C++23) |
插入一个元素范围 (公共成员函数) |
(C++23) |
在末尾添加一个元素范围 (公共成员函数) |
为容器赋值 (公共成员函数) | |
为容器赋值 (公共成员函数) |