std::string
std::string is an owning, resizable buffer of char. It stores a contiguous sequence, tracks its length, and frees the buffer in its destructor. It is the default type for mutable text you own.
Detailed Explanation
A C string is a char* and a convention that a 0 byte ends it. Length is a walk. Ownership is a comment. std::string is a class that owns its characters, knows size() in O(1), and can hold interior '\0' bytes because length is stored, not scanned.
The object is a value. Copying duplicates the characters (or shares them only if a library still uses copy-on-write, which the standard effectively forbade for std::string after C++11). Moving steals the buffer. const std::string& is the common read parameter; C++17 added std::string_view for a non-owning read that also accepts literals.
Small string optimisation (SSO) is why a short name does not hit the heap. Implementations keep a handful of characters (typically 15 on 64-bit libstdc++, 22 on libc++) inside the string object itself. Longer text allocates. sizeof(std::string) is about 24 or 32 bytes on 64-bit platforms, whether the text is short or long; the heap block is extra.
s[i] is unchecked. s.at(i) throws. s + t allocates a new string. s += t appends and may reallocate. reserve before a loop of appends. c_str() and data() return a pointer to a null-terminated buffer; that pointer dangles after any reallocation or after the string dies. data() was const-only until C++17; now a non-const data() lets you fill the buffer you already resized.
Encoding is not a property of std::string. It is bytes. UTF-8 text lives in a string by convention. size() is bytes, not code points. Indexing into the middle of a multibyte character is how you corrupt text. Use a Unicode library when you need graphemes. std::u8string (char8_t) marks UTF-8 in the type system from C++20 but is still a byte string.
std::string compares lexicographically as unsigned bytes on a well-behaved library. == is a length check plus a memcmp. It is not locale-aware collation.
Construction from a NULL const char* is undefined behaviour. Construction from a pointer and a count can include zeros. std::string(5, 'x') is five x's; std::string("x", 5) reads five bytes from a one-character literal and is a buffer overrun.
Prefer string when you own and may mutate. Prefer string_view when you only read and the buffer already lives somewhere. Prefer const char* only at C API edges.
Code Examples
#include <iostream>
#include <string>
int main() {
std::string name = "ada";
name += " lovelace";
std::cout << name << '\n'; // prints: ada lovelace
std::cout << name.size() << '\n'; // prints: 12
std::string id;
id.reserve(16);
id += "user-";
id += std::to_string(42);
std::cout << id << '\n'; // prints: user-42
}#include <iostream>
#include <string>
int main() {
std::string raw{"a\0b", 3}; // length 3, interior zero
std::cout << raw.size() << '\n'; // prints: 3
std::cout << raw.c_str()[0] << '\n'; // prints: a (c_str stops at the zero for C APIs)
}Common Gotchas
Holding `c_str()` across a mutation
`const char* p = s.c_str(); s += "x";` invalidates `p`. Copy the bytes, or finish using `p` first.
`size()` is bytes
a UTF-8 string of one emoji can have `size() == 4`. Do not use `s[i]` to step code points.