C++17 Early adopters: String_view

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.