名前空間
変種

std::basic_string<CharT,Traits,Allocator>::capacity

提供: cppreference.com
2020年2月18日 (火) 07:37時点におけるFruderica (トーク | 投稿記録)による版 (P0980R1)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
 
 
 
std::basic_string
 
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>
size_type capacity() const;
(C++11未満)
size_type capacity() const noexcept;
(C++11以上)
(C++20未満)
constexpr size_type capacity() const noexcept;
(C++20以上)

文字列が現在確保している空間の文字数を返します。

引数

(なし)

戻り値

現在確保されている記憶域の容量。

計算量

一定。

#include <iostream>
#include <string>

void show_capacity(std::string const& s)
{
    std::cout << "'" << s << "' has capacity " << s.capacity() << ".\n";
}

int main()
{
    std::string s{"Exemplar"};
    show_capacity(s);

    s += " is an example string.";
    show_capacity(s);
}

出力例:

'Exemplar' has capacity 15.
'Exemplar is an example string.' has capacity 30.

関連項目

文字数を返します
(パブリックメンバ関数) [edit]
記憶域を予約します
(パブリックメンバ関数) [edit]