Notes

Thoughts, write-ups, and things worth remembering.

std::string

#cpp#containers Updated Sep 16, 2026

A dynamically-sized, owning string type — manages its own memory, unlike a raw char*.

std::string s = "hello";
s += " world";              // grows automatically
s.length();                  // 11
s.substr(0, 5);               // "hello"
s.find("world");              // 6 (index), or std::string::npos if not found
s.c_str();                     // raw const char* for old C-style APIs

Gotcha: comparing with ==

if (s == "hello") { ... }   // works fine - std::string overloads ==

Unlike C-style strings, where strA == strB compares addresses, not contents (strcmp is what you’d need there instead).

C-style arrays
std::vector basics