void reserve( size_type new_cap = 0 ); | (1) | (until C++20) |
void reserve( size_type new_cap); | (1) | (since C++20) |
void reserve(); | (2) | (since C++20) (deprecated) |
std::basic_string
object of a planned change in size, so that it can manage the storage allocation appropriately.new_cap
is greater than the current capacity()
, new storage is allocated, and capacity()
is made equal or greater than new_cap
.
| (until C++20) |
| (since C++20) |
2) A call to reserve with no argument is a non-binding shrink-to-fit request. After this call, capacity() has an unspecified value greater than or equal to size() . | (since C++20) |
new_cap | - | new capacity of the string |
(none).
Throws std::length_error
if new_cap
is greater than max_size()
.
May throw any exceptions thrown by std::allocator_traits<Allocator>::allocate()
, such as std::bad_alloc
.
At most linear in the size()
of the string.
#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()); }
returns the number of characters that can be held in currently allocated storage (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/reserve