Namespaces
Variants

std::inplace_vector<T,N>::reserve

来自 zh.cppreference.net
 
 
 
 
static constexpr void reserve( size_type new_cap );
(自C++26起)

不做任何事情,除了可能会抛出 std::bad_alloc。增加容量(即内部存储大小)的请求被忽略,因为 std::inplace_vector<T, N> 是一个固定容量容器。

参数

new_cap - inplace_vector 的新容量,以元素数量计

复杂度

常量。

异常

当 new_cap > capacity ( ) 为 true 时抛出 std::bad_alloc 。

注释

此函数的存在是为了与类似向量的接口保持兼容。

示例

#include <cassert>
#include <inplace_vector>
#include <iostream>
int main()
{
    std::inplace_vector<int, 4> v{1, 2, 3};
    assert(v.capacity() == 4 && v.size() == 3);
    v.reserve(2); // 无操作
    assert(v.capacity() == 4 && v.size() == 3);
    try
    {
        v.reserve(13); // 抛出异常,因为请求的容量 > N;v 保持不变
    }
    catch(const std::bad_alloc& ex)
    {
        std::cout << ex.what() << '\n';
    }
    assert(v.capacity() == 4 && v.size() == 3);
}

可能的输出:

std::bad_alloc

参见

返回元素数量
(公开成员函数)
[static]
返回可能容纳的最大元素数
(公开静态成员函数)
改变存储的元素数量
(公开成员函数)
[static]
返回当前分配存储空间可容纳的元素数量
(公开静态成员函数)
通过释放未使用的内存来减少内存使用
(公开静态成员函数)