◐ Shell
clean mode source ↗

std::basic_string::operator basic_string_view — cppreference.com

De cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>

operator std::basic_string_view<CharT, Traits>() const noexcept;

constexpr operator std::basic_string_view<CharT, Traits>() const noexcept;

Retourne une std::basic_string_view, construite de manière équivalente à std::basic_string_view<CharT, Traits>(data(), size())

Paramètres

(aucun)

Valeur de retour

Une vue représentant l'entièreté du contenu de la chaîne de caractères.

Exemple

#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!
}

Résultat :

ラーメン
12 code units
4 code points

Voir aussi

Modèle:cpp/string/basic string view/dsc constructor