std::inplace_vector<T,N>:: assign_range
From cppreference.net
<
cpp
|
container
|
inplace vector
|
template
<
container-compatible-range
<
T
>
R
>
constexpr void assign_range ( R && rg ) ; |
(C++26 起) | |
将容器中的元素替换为 rg 中每个元素的副本。
| 本节内容尚不完整 |
范围 rg 中的每个迭代器都会被精确解引用一次。
如果 rg 与 * this 存在重叠,则行为未定义。
目录 |
参数
| rg | - |
一个
input_range
,其引用类型可转换为容器的元素类型
|
| 类型要求 | ||
|
-
|
||
-
T
无法从
*
ranges::
begin
(
rg
)
进行
原位构造
到
inplace_vector
中,则行为未定义
|
||
异常处理
- std::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); // 抛出异常:bad.size() > destination.capacity() } catch(const std::bad_alloc& ex) { std::cout << ex.what() << '\n'; } }
可能的输出:
std::bad_alloc
参见
|
插入元素范围
(公开成员函数) |
|
|
在末尾添加元素范围
(公开成员函数) |
|
|
为容器赋值
(公开成员函数) |
|
|
为容器赋值
(公开成员函数) |