On this page
  1. std::string
  2. Overview
  3. Usage
  4. Rule

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

Linked from

Related notes