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
;
|
(C++11以降 noexcept)
(C++20以降 constexpr) |
|
文字列が現在割り当てられているスペースの文字数を返します。
目次 |
パラメータ
(なし)
戻り値
現在割り当てられているストレージの容量、すなわち要素を格納するために利用可能なストレージ。
計算量
定数。
注記
アロケーターから取得されたが、いずれの要素の格納にも利用できないメモリ領域は、割り当てられたストレージには含まれません。ナル終端文字は std::basic_string の要素ではないことに注意してください。
例
このコードを実行
#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(); } }
出力例:
"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
関連項目
|
文字数を返す
(公開メンバ関数) |
|
|
ストレージを予約する
(公開メンバ関数) |