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

Explanation

Dynamic character array. Similar to std::vector<char> but with string-specific operations.

See std::string.

Memory: usually heap-allocated, but small strings might be stored inline (SSO).

Time complexity:

OperationComplexity
[], atO(1)
length()/size()/empty()O(1)
append()O(k) where k = appended length
substr()O(k) where k = substring length
compare()O(n)

Code

#include <print>
#include <string>

int main() {
  std::string symbolName = "_main";
  symbolName.append("_v2");

  std::println("Contains 'main': {}", symbolName.contains("main"));

  return 0;
}

View on GitHub.

Output

$ ./src/data-structures/build/std-string
Contains 'main': true