std::string in C++

From the C++ cheat sheet · Strings · verified Jul 2026

std::string

The owning, mutable string type.

cpp
// Quick Reference
#include <string>
std::string s = "hello";
s.size();  s.substr(1, 3);
s += " world";  s.find("world");
💡 std::string owns its data and has value semantics - assigning copies the contents.
⚡ find returns std::string::npos (not -1) when the substring is absent.
📌 std::stoi/stod parse numbers; std::to_string converts numbers to text.
🟢 Index with s[i] for speed, or s.at(i) for bounds-checked access (throws).
strings

More C++ tasks

Back to the full C++ cheat sheet