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

履歴 編集

function
<string_view>

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

constexpr const_pointer data() const noexcept;

概要

文字配列表現を取得する。

戻り値

*thisが保持している、参照している文字配列へのポインタを返す。

例外

投げない

備考

文字列長はsubstr()メンバ関数やコンストラクタなどで変更できるが、それらは参照の範囲を限定するのみである。この関数によって返される文字配列へのポインタは、参照範囲の終端にヌル文字は挿入しないので注意すること。

#include <iostream>
#include <string_view>

int main()
{
  std::string_view sv = "Hello World";
  const char* s = sv.data();
  std::cout << "a : " << s << std::endl;

  // 部分文字列
  std::string_view sv2 = sv.substr(0, 5);
  const char* s2 = sv2.data();
  std::cout << "b : " << sv2 << std::endl;
  std::cout << "c : " << s2 << std::endl; // 参照位置から全体が出力される
}

出力

a : Hello World
b : Hello
c : Hello World

バージョン

言語

  • C++17

処理系