Skip to content
Tutorials Center
My learning

Topics

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.

C++
#include <string_view>

void greet(std::string_view name);

Try it

C++ Slicing without copying

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.

C++ Converting and the dangling trap

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

C++
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::string is a classic bug.
  • Do not store views in long-lived objects unless something else guarantees the owner outlives them.
  • Conversion to std::string is explicit (std::string{v}); the other direction is implicit, so a function taking string_view accepts std::string, literals and const char* with no copy.
  • No null terminator guarantee. std::string objects built from views, std::atoi, fopen and other C APIs need a terminated buffer; copy into a std::string first.
  • Literals: "text"sv creates 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 equivalent std::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&.