[c++] std::experimental::string_view

In C++, std::experimental::string_view is a non-owning view over a sequence of characters. It provides a read-only view of a contiguous sequence of characters and is particularly useful for passing substrings of strings to functions without the need to create a new string.

Features and Benefits

Usage

Creating std::experimental::string_view

You can create a std::experimental::string_view from a const char* or a std::string:

std::string_view view1 = "Hello, World!";
std::string_view view2 = std::string("Hello, C++");

Operations on std::experimental::string_view

Example

void print_string_view(std::string_view view) {
    for (char c : view) {
        std::cout << c;
    }
}

int main() {
    std::string str = "Hello, C++";
    print_string_view(str);
    return 0;
}

Availability

std::experimental::string_view is available in the C++17 standard. To use it, ensure that you compile with the appropriate version of the C++ standard library that supports C++17.

For more details, refer to the official C++ reference.

Using std::experimental::string_view can improve code efficiency and reduce unnecessary string copies, making it a valuable addition to C++ when working with string views.