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

履歴 編集

function
<string_view>

std::basic_string_view::size(C++17)

constexpr size_type size() const noexcept;

概要

文字列の長さを取得する。

戻り値

保持している文字列の長さを返す。

例外

投げない

備考

この関数は、length()メンバ関数と同じ効果を持つ。size()はコンテナインタフェースのために用意されているものだが、どちらを使用してもよい。

#include <cassert>
#include <iostream>
#include <string_view>

int main()
{
  std::string_view sv1{"Hello World"};
  std::string_view sv2 = sv1.substr(0, 5);

  assert(sv1.size() == 11);
  assert(sv2.size() == 5);

  std::cout << '[' << sv1 << ']' << std::endl;
  std::cout << '[' << sv2 << ']' << std::endl;
}

出力

[Hello World]
[Hello]

バージョン

言語

  • C++17

処理系