最終更新日時(UTC):
が更新

履歴 編集

function
<string>

std::basic_string::capacity

size_type capacity() const;                    // (1) C++03
size_type capacity() const noexcept;           // (1) C++11
constexpr size_type capacity() const noexcept; // (1) C++20

概要

メモリを再確保せずに格納できる最大の要素数を取得する。

戻り値

メモリを再確保せずに格納できる最大の要素数

例外

投げない

計算量

定数時間

#include <iostream>
#include <string>

int main()
{
  std::string s;
  s.reserve(3);

  // 確保したサイズを確認
  std::size_t cap = s.capacity();
  std::cout << cap << std::endl;
}

出力例

3

参照