std::vector<T,Allocator>::reserve
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 起) |
[編輯] 返回值
(無)
[編輯] 異常
- 如果 new_cap > max_size(),則丟擲 std::length_error。
- 由
Allocator::allocate()
丟擲的任何異常(通常是 std::bad_alloc)。
如果丟擲異常,此函式不產生任何效果(強異常保證)。
如果 |
(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) | |
(DR*) |
透過釋放未使用的記憶體來減少記憶體使用 (public member function) |