命名空间
变体
操作

std::vector<T,Allocator>::reserve

来自 cppreference.cn
< cpp‎ | 容器‎ | vector
 
 
 
 
void reserve( size_type new_cap );
(C++20 起为 constexpr)

将 vector 的容量(vector 可以容纳而不必重新分配的元素总数)增加到大于或等于 new_cap 的值。如果 new_cap 大于当前的 capacity(),则会分配新的存储空间;否则,函数不执行任何操作。

reserve() 不会改变 vector 的大小。

如果 new_cap 大于 capacity(),则所有迭代器(包括 end() 迭代器)以及对元素的任何引用都将失效。否则,没有迭代器或引用会失效。

调用 reserve() 后,除非插入操作将导致 vector 的大小大于 capacity() 的值,否则插入不会触发重新分配。

目录

[编辑] 参数

new_cap - vector 的新容量,以元素数量表示
类型要求
-
T 必须满足可移动插入*this 的要求。(C++11 起)

[编辑] 返回值

(无)

[编辑] 异常

如果抛出异常,此函数不产生任何效果(强异常保证)。

如果 T 的移动构造函数不是 noexcept 且 T 不可复制插入*this,则 vector 将使用抛出移动构造函数。如果它抛出,则保证失效,效果未指定。

(C++11 起)

[编辑] 复杂度

至多与容器的 size() 成线性关系。

[编辑] 注意

正确使用 reserve() 可以避免不必要的重新分配,但不恰当地使用 reserve()(例如,在每次调用 push_back() 之前都调用它)实际上可能会增加重新分配的次数(导致容量线性增长而非指数增长),从而导致计算复杂度增加和性能下降。例如,一个通过引用接收任意 vector 并向其添加元素的函数通常不应该对 vector 调用 reserve(),因为它不知道 vector 的使用特性。

当插入一个范围时,insert() 的范围版本通常更可取,因为它保留了正确的容量增长行为,与 reserve() 后跟一系列 push_back() 不同。

reserve() 不能用于减少容器的容量;为此提供了 shrink_to_fit()

[编辑] 示例

#include <cstddef>
#include <iostream>
#include <new>
#include <vector>
 
// minimal C++11 allocator with debug output
template<class Tp>
struct NAlloc
{
    typedef Tp value_type;
 
    NAlloc() = default;
    template<class T>
    NAlloc(const NAlloc<T>&) {}
 
    Tp* allocate(std::size_t n)
    {
        n *= sizeof(Tp);
        Tp* p = static_cast<Tp*>(::operator new(n));
        std::cout << "allocating " << n << " bytes @ " << p << '\n';
        return p;
    }
 
    void deallocate(Tp* p, std::size_t n)
    {
        std::cout << "deallocating " << n * sizeof *p << " bytes @ " << p << "\n\n";
        ::operator delete(p);
    }
};
 
template<class T, class U>
bool operator==(const NAlloc<T>&, const NAlloc<U>&) { return true; }
 
template<class T, class U>
bool operator!=(const NAlloc<T>&, const NAlloc<U>&) { return false; }
 
int main()
{
    constexpr int max_elements = 32;
 
    std::cout << "using reserve: \n";
    {
        std::vector<int, NAlloc<int>> v1;
        v1.reserve(max_elements); // reserves at least max_elements * sizeof(int) bytes
 
        for (int n = 0; n < max_elements; ++n)
            v1.push_back(n);
    }
 
    std::cout << "not using reserve: \n";
    {
        std::vector<int, NAlloc<int>> v1;
 
        for (int n = 0; n < max_elements; ++n)
        {
            if (v1.size() == v1.capacity())
                std::cout << "size() == capacity() == " << v1.size() << '\n';
            v1.push_back(n);
        }
    }
}

可能的输出

using reserve: 
allocating 128 bytes @ 0xa6f840
deallocating 128 bytes @ 0xa6f840
 
not using reserve: 
size() == capacity() == 0
allocating 4 bytes @ 0xa6f840
 
size() == capacity() == 1
allocating 8 bytes @ 0xa6f860
deallocating 4 bytes @ 0xa6f840
 
size() == capacity() == 2
allocating 16 bytes @ 0xa6f840
deallocating 8 bytes @ 0xa6f860
 
size() == capacity() == 4
allocating 32 bytes @ 0xa6f880
deallocating 16 bytes @ 0xa6f840
 
size() == capacity() == 8
allocating 64 bytes @ 0xa6f8b0
deallocating 32 bytes @ 0xa6f880
 
size() == capacity() == 16
allocating 128 bytes @ 0xa6f900
deallocating 64 bytes @ 0xa6f8b0
 
deallocating 128 bytes @ 0xa6f900

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 329 C++98 如果插入操作
使 vector 的大小大于
最近一次调用 reserve() 中指定的大小
可能会触发重新分配
仅当 vector 的大小
变得大于 capacity() 时才触发
LWG 2033 C++11 未要求 T移动插入 需要

[编辑] 参阅

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