String_view
String_view is a lightweight object provided by C++17 for handling read-only strings.
- You can convert a string to a String_view object by calling the String_view constructor.
- String_view are typically used for function parameter types and can be substituted for const char* and const string&.
- The member function of String_view is similar to the external interface as a string, but contains only the part that reads the contents of the string.
- The suffix of the string_view literal is SV. (string literal suffix is s)
Instance
#include <string>#include <iostream>using namespaceStd//void process (const char* SV)//void process (const string& SV)voidProcess (String_view sv) {cout << sv << Endl; for(CharCH:SV) cout << ch; cout << sv[2] << Endl;}intMain () {String_view SV ="Hello"Sv cout << sv << Endl; String_view Sv2 ="Hello"; cout << sv2 << Endl; String_view Sv3 ="Hello"S cout << sv3 << Endl; String_view SV4 ("Hello",4); cout << sv4 << Endl; Process"Hello"); Process"Hello"s);}/*HelloHelloHelloHellHellohellolHellohellol*/
C++17 Early adopters: String_view