std::basic_string::reserve(3)
Bestie, welcome to the C++ Standard Libary manual β yes, that's the header's spelling, not me π β and we're on the std::basic_string::reserve(3) page. Buckle up, this one's lowkey about storage glow-ups β¨
π NAME π
std::basic_string::reserve β std::basic_string::reserve β that's the whole name, bestie π
β¨ Synopsis β¨
Here's the signature timeline, no cap:
void reserve( size_type new_cap = 0 ); (until C++20)
constexpr void reserve( size_type new_cap ); (since C++20)
(1) (since C++20)
void reserve(); (2) (deprecated in
C++20)
(removed in
C++26)
The (1) and (2) markers are overload labels, bestie β don't let the spacing gaslight you π«Ά
Okay so here's the actual tea β
- 1) Informs a
std::basic_stringobject of a planned change in size, so that it can manage the storage allocation appropriately.- If
new_capis greater than the currentcapacity(), new storage is allocated, andcapacity()is made equal or greater thannew_cap. - If
new_capis less than the currentcapacity(), this is a non-binding shrink request. - If
new_capis less than the currentsize(), this is a non-binding (until C++20) shrink-to-fit request equivalent toshrink_to_fit()(since C++11). - If
new_capis less than or equal to the currentcapacity(), there is no effect. (since C++20)
- If
- If a capacity change takes place, all iterators and references, including the past-the-end iterator, are invalidated.
- 2) A non-binding shrink-to-fit request. After this call,
capacity()has an unspecified value greater than or equal tosize().
πΈ Parameters πΈ
new_capβ new capacity of the string.
π«Ά Return value π«Ά
Literally (none), bestie. It's giving "I did the work, I don't need a trophy" π
π Exceptions π
- Throws
std::length_errorifnew_capis greater thanmax_size(). - May throw any exceptions thrown by
std::allocator_traits<Allocator>::allocate(), such asstd::bad_alloc. - If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee). Iconic behavior, honestly β¨
β‘ Complexity β‘
At most linear in the size() of the string.
π» Example π»
// Run this code β yes bestie, run it:
#include <cassert>
#include <iostream>
#include <string>
int main()
{
std::string s;
std::cout << "1) Initially: " << s.capacity() << '\n';
const std::string::size_type new_cap{101u};
s.reserve(new_cap);
assert(s.capacity() >= new_cap);
std::cout << "2) After reserve(" << new_cap << "): " << s.capacity() << '\n';
// observing the capacity growth factor
auto cap{s.capacity()};
for (int check{}; check != 4; ++check)
{
while (cap == s.capacity())
s += '$';
cap = s.capacity();
std::cout << (3) + check << ") Capacity: " << cap << '\n';
}
// s.reserve(); // deprecated/removed in C++20/26, use:
s.shrink_to_fit();
std::cout << "7) After shrink_to_fit: " << s.capacity() << '\n';
}
π Possible output π
1) Initially: 15
2) After reserve(101): 101
3) Capacity: 202
4) Capacity: 404
5) Capacity: 808
6) Capacity: 1616
7) After shrink_to_fit: 809
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 847β applied toC++98; behavior as published: there was no exception safety guarantee; correct behavior: added strong exception safety guarantee.
π See also π
capacityβ returns the number of characters that can be held in currently allocated storage (public member function).resizeβ changes the number of characters stored (public member function).
Source/version/date check, bestie: http://cppreference.com, 2024.06.10, page std::basic_string::reserve(3) β and that's the whole manual page, periodt πβ¨π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms