std::inplace_vector<T,N>::assign_range
来自 cppreference.cn
< cpp | 容器 | 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)。否则,行为未定义。 |
[编辑] 返回值
(无)
异常
- 若 std::ranges::distance(rg) > capacity(),则为 bad_alloc。
- 插入元素的初始化可能抛出的任何异常。
[编辑] 示例
运行此代码
#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
[编辑] 参阅
插入元素范围 (公开成员函数) | |
添加一个元素范围到结尾 (公开成员函数) | |
将值赋给容器 (公开成员函数) | |
将值赋给容器 (公开成员函数) |