C++ Reference ยท Containers
C++ std::string_view class
Reference for std::string_view: a non-owning view of characters, cheap substr, remove_prefix, comparison, conversions to std::string and dangling-view traps.
std::string_view (C++17, header <string_view>) is a pair of a pointer and a length that refers to characters stored somewhere else: a string literal, a std::string, a character array or a slice of a file buffer. It owns nothing, allocates nothing and copying it costs two words. That makes it the best parameter type for functions that only read text.
Because it does not own the characters, a view is only valid while the underlying storage lives and is not modified. It also does not have to be null-terminated, so never pass view.data() to a C function that expects a C string unless you know the view covers the whole terminated buffer.
#include <string_view> void greet(std::string_view name);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
constexpr std::string_view(); constexpr std::string_view(const char* s); constexpr std::string_view(const char* s, std::size_t count); template<class R> constexpr explicit std::string_view(R&& range); // C++23
| Name | Description |
|---|---|
s |
Pointer to the first character. For the one-argument form the length is found with strlen; passing a null pointer is undefined behavior (deleted for nullptr in C++23). |
count |
Number of characters in the view. The characters need not be null-terminated. |
range |
A contiguous range of char (C++23), for example a std::vector<char>. |
Members: size, length, empty, data, front, back, operator[], at, substr, remove_prefix, remove_suffix, swap, compare, find family, starts_with, ends_with, contains (C++23), copy, iterators. There are no modifying operations: the characters are read-only.
Returns: substr returns another view (O(1), unlike std::string::substr). find returns an index or npos.
Errors / undefined behavior: at and substr throw std::out_of_range for a bad position. remove_prefix(n) with n > size() and front() on an empty view are undefined.
Usage notes
- Dangling is the main risk. A view of a temporary string, of a local that has gone out of scope, or of a string that is then modified or reallocated points at invalid memory. Returning a view of a local
std::stringis a classic bug. - Do not store views in long-lived objects unless something else guarantees the owner outlives them.
- Conversion to
std::stringis explicit (std::string{v}); the other direction is implicit, so a function takingstring_viewacceptsstd::string, literals andconst char*with no copy. - No null terminator guarantee.
std::stringobjects built from views,std::atoi,fopenand other C APIs need a terminated buffer; copy into astd::stringfirst. - Literals:
"text"svcreates a view with the correct length, including embedded nulls. - Hashing and maps:
std::hash<std::string_view>exists and yields the same hash as the equivalentstd::string, which enables heterogeneous lookup. - Operators:
==,<=>and<<work as for strings;+is not provided. - Performance: parameters passed by value are cheap (usually two registers); do not write
const std::string_view&.
Related
std::string,std::span,std::vector- Lessons: std::string_view, Strings