C++ › Strings & string_view

What is std::string_view and why was it added?

Beginner

Answer

std::string_view (C++17) is a non-owning, read-only view over a contiguous sequence of characters (a pointer plus length). It avoids copying when you only need to read a string, and can refer to std::string, C-strings, or substrings uniformly. It's ideal for function parameters that just inspect text.
void log(std::string_view msg);   // no copy for any string type
log("literal"); log(myString);
Real-world example Changing a parameter from const std::string& to string_view lets callers pass literals without constructing a string.

Common follow-ups: Does string_view own its data? | Why use it for parameters?