On this page
std::string
Prev: std::vector | Up: containers | Next: std::string_view
Rel: std::string_view, std::format & std::print
Ref: cppreference — std::basic_string
Tags: #public #prog #cpp #stdlib #containers #tier/core
Overview
std::string (<string>) is an owning, mutable, null-terminated contiguous sequence of char. Modern implementations use Small String Optimization (SSO), storing short strings (~15–22 bytes) inline inside the object with zero heap allocation.
Usage
#include <string>
std::string s = "hello";
s += " world";
bool ok = s.starts_with("hello"); // C++20
bool has = s.contains("world"); // C++23
const char* c_api = s.c_str(); // guaranteed null-terminated for C APIs
Rule
- Use
std::stringwhen you own or mutate text; prefer std::string_view for read-only function parameters.
Linked from
Related notes
- tuts/cpp/stdlib/containers/string_view Linked here; links to this page; shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/vector Linked here; links to this page; shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers Linked here; links to this page; shared tags: #containers, #cpp, #prog, #stdlib
- tuts/cpp/stdlib/io_and_filesystem/format_and_print Linked here; links to this page; shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/io_and_filesystem links to this page; shared tags: #cpp, #prog, #stdlib
- tuts/cpp/stdlib/containers/array shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/expected shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/map shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/optional shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/span shared tags: #containers, #cpp, #prog, #stdlib, #tier