std::inplace_vector<T,N>::reserve
來自 cppreference.com
< cpp | 容器 | inplace_vector
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) |