名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
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]