Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::string_view

Explanation

Non-owning view over character data. Avoids copying strings.

See std::string_view.

Time complexity:

OperationComplexity
[], front(), back()O(1)
size()/empty()O(1)
compare()O(n)

Code

#include <print>
#include <string_view>

void printSymbol(std::string_view name) { std::println("Symbol: {}", name); }

int main() {
  std::string str = "_main";
  const char *cstr = "_helper";

  printSymbol(str);
  printSymbol(cstr);

  return 0;
}

View on GitHub.

Output

$ ./src/data-structures/build/std-string_view
Symbol: _main
Symbol: _helper