std::basic_string<CharT,Traits,Allocator>::reserve - cppreference.com
提供: cppreference.com
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
| (1) | ||
|
|
(C++20未満) | |
|
|
(C++20以上) | |
|
|
(2) | (C++20以上) (非推奨) |
1) 記憶域の確保を適切に管理できるように、サイズ変更の予定を std::basic_string に知らせます。
new_capが現在の capacity() より大きい場合、新しい記憶域が確保され、 capacity() がnew_capより大きくまたは等しくなります。
|
(C++20未満) |
|
(C++20以上) |
容量変更が行われた場合、すべてのイテレータと参照 (終端イテレータを含む) が無効化されます。
|
2) 引数なしの |
(C++20以上) |
引数
戻り値
(なし)
例外
new_cap が max_size() より大きい場合、 std::length_error を投げます。
std::bad_alloc などの std::allocator_traits<Allocator>::allocate() によって投げられるあらゆる例外も投げるかもしれません。
計算量
文字列の size() にほぼ比例。
例
#include <cassert> #include <string> int main() { std::string s; std::string::size_type new_capacity{ 100u }; assert(new_capacity > s.capacity()); s.reserve(new_capacity); assert(new_capacity <= s.capacity()); }