名前空間
変種
操作

「cpp/string/basic string/operator basic string view」の版間の差分

提供: cppreference.com
< cpp‎ | string‎ | basic string
(P0980R1)
62行: 62行:
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|en|fr|zh}}
+
{{langlinks|en|fr|zh}}

2020年3月20日 (金) 09:54時点における版

 
 
 
std::basic_string
 
operator std::basic_string_view<CharT, Traits>() const noexcept;
(C++17以上)
(C++20未満)
constexpr operator std::basic_string_view<CharT, Traits>() const noexcept;
(C++20以上)

std::basic_string_view<CharT, Traits>(data(), size()) によって行われたかのように構築された、 std::basic_string_view を返します。

目次

引数

(なし)

戻り値

文字列全体の内容を表す文字列ビュー。

#include <iostream>
#include <string>
#include <string_view>
 
void show_wstring_size(std::wstring_view wcstr_v)
{
  std::cout << wcstr_v.size() << " code points\n";
}
 
int main()
{
  std::string cppstr = "ラーメン";   // narrow string
  std::wstring wcstr = L"ラーメン";  // wide string
 
  // Implicit conversion from string to string_view
  // via std::string::operator string_view:
  std::string_view cppstr_v = cppstr;
 
  std::cout << cppstr_v << '\n'
            << cppstr_v.size() << " code units\n";
 
  // Implicit conversion from wstring to wstring_view
  // via std::wstring::operator wstring_view:
  show_wstring_size(wcstr);
 
  // Warning:
  // It is the programmer's responsibility to ensure that std::string_view 
  // does not outlive the pointed-to string! 
 
  std::string_view BAD(std::string("a temporary string")); // holds a dangling pointer!
}

出力:

ラーメン
12 code units
4 code points

関連項目

basic_string_view を構築します
(std::basic_string_view<CharT,Traits>のパブリックメンバ関数) [edit]