std::basic_...ng::capacity(3)
πΈ NAME πΈ
std::basic_string::capacity β literally std::basic_string::capacity, bestie. This is the std::basic_...ng::capacity(3) page from the C++ Standard Libary (manual name, yes, that spelling is the manual name, iykyk π«Ά).
β¨ Synopsis β¨
size_type capacity() const; (noexcept since C++11)
(constexpr since C++20)
Returns the number of characters that the string has currently allocated space for. That's the whole vibe π .
π Parameters π
(none) β zero parameters. Nada. Not a single one, bestie.
π Return value π
The capacity of the currently allocated storage, i.e. the storage available for storing elements. That's it. That's the return value. Periodt π«.
π¦ Complexity π¦
Constant. Zero drama, constant energy, literally.
πΈ Notes πΈ
Okay, important note time: memory locations obtained from the allocator but not available for storing any element are not counted in the allocated storage. Also, note that the null terminator is not an element of the std::basic_string. Not me reminding you about the null terminator π.
π₯ Example π₯
The page says // Run this code β so go run it, bestie π
#include <iomanip>
#include <iostream>
#include <string>
void show_capacity(std::string const& s)
{
std::cout << std::quoted(s) << " has capacity " << s.capacity() <<
".\n";
}
int main()
{
std::string s{"Exemplar"};
show_capacity(s);
s += " is an example string.";
show_capacity(s);
s.clear();
show_capacity(s);
std::cout << "\nDemonstrate the capacity's growth policy."
"\nSize: Capacity: Ratio:\n" << std::left;
std::string g;
auto old_cap{g.capacity()};
for (int mark{}; mark != 5; ++mark)
{
while (old_cap == g.capacity())
g.push_back('.');
std::cout << std::setw( 7) << g.size()
<< std::setw(11) << g.capacity()
<< std::setw(10) << g.capacity() /
static_cast<float>(old_cap) << '\n';
old_cap = g.capacity();
}
}
π« Possible output π«
"Exemplar" has capacity 15.
"Exemplar is an example string." has capacity 30.
"" has capacity 30.
Demonstrate the capacity's growth policy.
Size: Capacity: Ratio:
16 30 2
31 60 2
61 120 2
121 240 2
241 480 2
π See also π
sizeβ returns the number of characterslengthβ (public member function)reserveβ reserves storage (public member function)
Source/version: http://cppreference.com π Page date: 2024.06.10 β Footer title: std::basic_...ng::capacity(3) π Bye bestie, go check that capacity π
π¦ 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