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
- Non-owning View: Unlike
std::string
,std::experimental::string_view
does not own the underlying character sequence. - Cost-effective: Since it doesn’t allocate memory or make any copies, it’s efficient for passing strings to functions and for comparisons.
- Compatibility: It’s compatible with existing code that accepts
std::string
and provides a safer, more efficient alternative to pointer/length pairs.
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
- Accessing characters using
operator[]
- Obtaining the length using
size()
orlength()
- Substring operations using
substr()
- Comparisons using
operator==
andoperator!=
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.