std::inplace_vector<T,N>::inplace_vector
来自cppreference.net
constexpr inplace_vector() noexcept;
|
(1) | (自 C++26) |
constexpr explicit inplace_vector( size_type count );
|
(2) | (自 C++26) |
constexpr inplace_vector( size_type count, const T& value );
|
(3) | (自 C++26) |
template< class InputIt >
constexpr inplace_vector( InputIt first, InputIt last );
|
(4) | (自 C++26) |
template< /*container-compatible-range*/<T> R >
constexpr inplace_vector( std::from_range_t, R&& rg );
|
(5) | (自 C++26) |
constexpr inplace_vector( const inplace_vector& other );
|
(6) | (自 C++26) |
constexpr inplace_vector( inplace_vector&& other )
noexcept(N == 0 || std::is_nothrow_move_constructible_v<T>);
|
(7) | (自 C++26) |
constexpr inplace_vector( std::initializer_list<T> init );
|
(8) | (自 C++26) |
从各种数据源构造一个新的inplace_vector。
1) 构造一个空的
inplace_vector。3) 构造一个
inplace_vector,包含count个值为value的元素副本。4) 构造一个
inplace_vector,其内容为范围[first, last)中的元素。5) 构造一个
inplace_vector,其内容为范围rg中的元素。 关于
/*container-compatible-range*/的定义,请参阅ranges::to。 如果
ranges::size(rg)是常量表达式且大于N,则程序非良构。6) 复制构造函数。构造一个
inplace_vector,其内容为other的副本。7) 移动构造函数。构造一个
inplace_vector,其内容使用移动语义从other获取。8) 构造一个
inplace_vector,其内容为初始化列表init。参数
| count | - | 容器的大小 |
| value | - | 用于初始化容器元素的值 |
| first, last | - | 定义待复制元素源 范围 的迭代器对 |
| rg | - | 用于初始化容器元素的值范围 |
| other | - |
用作容器元素初始化源的另一个
inplace_vector
|
| init | - | 用于初始化容器元素的初始化列表 |
| 类型要求 | ||
-
T
必须满足
DefaultInsertable
要求才能使用重载(2,3)
|
||
复杂度
1)
常量。
2,3)
与
count
呈线性关系。
4)
与
std::
distance
(
first, last
)
呈线性关系。
5)
与
std
::
ranges::
distance
(
rg
)
呈线性关系。
6,7)
与
other
的大小成线性关系。
8)
与
init
的大小呈线性关系。
异常
示例
运行此代码
#include <cassert>
#include <initializer_list>
#include <inplace_vector>
#include <new>
#include <print>
#include <ranges>
int main()
{
std::inplace_vector<int, 4> v1; // overload (1)
assert(v1.size() == 0 && v1.capacity() == 4);
std::inplace_vector<int, 0> v2; // overload (1), N == 0 is allowed
assert(v2.size() == 0 && v2.capacity() == 0);
std::inplace_vector<int, 5> v3(3); // overload (2)
assert(v3.size() == 3 && v3.capacity() == 5);
std::println("v3 = {}", v3);
try
{
std::inplace_vector<int, 3> v(4); // overload (2), throws: count > N
}
catch(const std::bad_alloc& ex1)
{
std::println("ex1.what(): {}", ex1.what());
}
std::inplace_vector<int, 5> v4(3, 42); // overload (3)
assert(v4.size() == 3 && v4.capacity() == 5);
std::println("v4 = {}", v4);
try
{
std::inplace_vector<int, 3> v(4, 2); // overload (3), throws: count > N
}
catch(const std::bad_alloc& ex2)
{
std::println("ex2.what(): {}", ex2.what());
}
const auto init = {1, 2, 3};
std::inplace_vector<int, 4> v5(init.begin(), init.end()); // overload (4)
assert(v5.size() == 3 && v5.capacity() == 4);
std::println("v5 = {}", v5);
std::inplace_vector<int, 4> v6(std::from_range, init); // overload (5)
assert(v6.size() == 3 && v6.capacity() == 4);
std::println("v6 = {}", v6);
std::inplace_vector<int, 4> v7(v6); // overload (6)
assert(v7.size() == 3 && v7.capacity() == 4);
std::println("v7 = {}", v7);
assert(v6.size() == 3);
std::inplace_vector<int, 4> v8(std::move(v6)); // overload (7)
// Note that after the move v6 is left in valid but indeterminate state.
assert(v8.size() == 3 && v8.capacity() == 4);
std::println("v8 = {}", v8);
std::inplace_vector<int, 4> v9(init); // overload (8)
assert(v9.size() == 3 && v9.capacity() == 4);
std::println("v9 = {}", v9);
try
{
std::inplace_vector<int, 2> v(init); // overload (8), throws: init.size() > N
}
catch(const std::bad_alloc& ex3)
{
std::println("ex3.what(): {}", ex3.what());
}
}
可能的输出:
v3 = [0, 0, 0]
ex1.what(): std::bad_alloc
v4 = [42, 42, 42]
ex2.what(): std::bad_alloc
v5 = [1, 2, 3]
v6 = [1, 2, 3]
v7 = [1, 2, 3]
v8 = [1, 2, 3]
v9 = [1, 2, 3]
ex3.what(): std::bad_alloc
参见
|
为容器赋值
(公开成员函数) |
|
|
[static]
|
返回当前分配存储空间可容纳的元素数量
(公开静态成员函数) |
|
直接访问底层连续存储空间
(公开成员函数) |
|
|
返回元素数量
(公开成员函数) |
|
|
(C++17)
(C++20)
|
返回容器或数组的大小
(函数模板) |
|
(C++17)
|
获取底层数组的指针
(函数模板) |