std::inplace_vector<T,N>::assign_range
来自 cppreference.cn
< cpp | container | inplace vector
template< container-compatible-range<T> R > constexpr void assign_range( R&& rg ); |
(自 C++26 起) | |
用 rg 中每个元素的副本替换容器中的元素。
本节尚不完整 |
范围 rg 中的每个迭代器恰好被解引用一次。
如果 rg 与容器重叠,则行为未定义。
目录 |
[编辑] 参数
rg | - | 一个 input_range ,其引用类型可转换为容器的元素类型 |
类型要求 | ||
-std::assignable_from<T&, ranges::range_reference_t<R>> 必须被建模。否则,程序是非良构的。 | ||
-T 必须是从 *ranges::begin(rg) 原位构造 (EmplaceConstructible) 到容器中 的 EmplaceConstructible。否则,行为未定义。 |
[编辑] 返回值
(无)
异常
- bad_alloc,如果 std::ranges::distance(rg) > capacity()。
- 插入元素的初始化抛出的任何异常。
[编辑] 示例
运行此代码
#include <algorithm> #include <cassert> #include <initializer_list> #include <inplace_vector> #include <iostream> #include <new> int main() { const auto source = {1, 2, 3}; std::inplace_vector<int, 4> destination{4, 5}; destination.assign_range(source); assert(std::ranges::equal(destination, source)); try { const auto bad = {-1, -2, -3, -4, -5}; destination.assign_range(bad); // throws: bad.size() > destination.capacity() } catch(const std::bad_alloc& ex) { std::cout << ex.what() << '\n'; } }
可能的输出
std::bad_alloc
[编辑] 参见
插入一个元素范围 (公共成员函数) | |
将一个元素范围添加到末尾 (公共成员函数) | |
为容器赋值 (公共成员函数) | |
为容器赋值 (公共成员函数) |