Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::operator basic_string_view

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      operatorstd::basic_string_view<CharT, Traits>()constnoexcept;
      (since C++17)
      (constexpr since C++20)

      Returns astd::basic_string_view, constructed as if bystd::basic_string_view<CharT, Traits>(data(), size()).

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      A string view representing the entire contents of the string.

      [edit]Notes

      It is the programmer's responsibility to ensure that the resulting string view does not outlive the string.

      std::string get_string();int f(std::string_view sv); int x= f(get_string());// OKstd::string_view sv= get_string();// Bad: holds a dangling pointer

      [edit]Example

      Run this code
      #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 stringstd::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);}

      Output:

      ラーメン12 code units4 code points

      [edit]See also

      constructs abasic_string_view
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator_basic_string_view&oldid=171121"

      [8]ページ先頭

      ©2009-2025 Movatter.jp