◐ Shell
clean mode source ↗

std::basic_string<CharT,Traits,Allocator>::capacity - cppreference.com

提供: cppreference.com

<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.

関連項目