命名空间
变体
操作

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

来自 cppreference.cn
 
 
 
 
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

[编辑] 注意

此函数为了与类似 vector 的接口兼容而存在。

[编辑] 示例

#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); // does nothing
    assert(v.capacity() == 4 && v.size() == 3);
 
    try
    {
        v.reserve(13); // throws, because requested capacity > N; v is left unchanged
    }
    catch(const std::bad_alloc& ex)
    {
        std::cout << ex.what() << '\n';
    }
    assert(v.capacity() == 4 && v.size() == 3);
}

可能的输出

std::bad_alloc

[编辑] 参阅

返回元素数量
(public member function) [编辑]
[静态]
返回元素的最大可能数量
(public static member function) [编辑]
更改存储的元素数量
(public member function) [编辑]
[静态]
返回当前已分配存储空间中可容纳的元素数量
(public static member function) [编辑]
通过释放未使用的内存来减少内存使用
(public static member function) [编辑]