std::basic_string<CharT,Traits,Allocator>:: capacity
From cppreference.net
<
cpp
|
string
|
basic string
C++
Strings library
| Classes | ||||
|
(C++17)
|
||||
std::basic_string
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
size_type capacity
(
)
const
;
|
(noexcept seit C++11)
(constexpr seit C++20) |
|
Gibt die Anzahl der Zeichen zurück, für die der String derzeit Speicherplatz reserviert hat.
Inhaltsverzeichnis |
Parameter
(keine)
Rückgabewert
Kapazität des aktuell zugewiesenen Speichers, d.h. der für das Speichern von Elementen verfügbare Speicher.
Komplexität
Konstante.
Hinweise
Speicherbereiche, die vom Allokator bezogen, aber nicht zur Speicherung eines Elements verfügbar sind, werden nicht zum allokierten Speicher gezählt. Beachten Sie, dass der Nullterminator kein Element des std::basic_string ist.
Beispiel
Diesen Code ausführen
#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(); } }
Mögliche Ausgabe:
"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
Siehe auch
|
gibt die Anzahl der Zeichen zurück
(öffentliche Elementfunktion) |
|
|
reserviert Speicher
(öffentliche Elementfunktion) |