C++ std::string Functions Cheat Sheet
1. Creation & Initialization
----------------------------
string s1 = "Hello";
string s2("World");
string s3(s1); // copy
string s4(5, 'x'); // "xxxxx"
2. Input & Output
-----------------
cin >> s1; // until space
getline(cin, s1); // full line
cout << s1 << endl;
3. Size & Capacity
------------------
[Link](); // length
[Link](); // same as size()
[Link](); // check empty
[Link](); // clear string
4. Accessing Characters
-----------------------
s1[0]; // first char
[Link](0); // first char (bounds check)
[Link](); // first char
[Link](); // last char
5. Modifying Strings
--------------------
s1 += " World"; // append
[Link]("!!!"); // append
s1.push_back('A'); // add char
s1.pop_back(); // remove last char
[Link](3, "XYZ"); // insert at index
[Link](2, 4); // erase from index
[Link](2, 3, "abc"); // replace chars
reverse([Link](), [Link]()); // reverse
6. Searching
------------
[Link]("llo"); // index or npos
[Link]("l"); // from end
s1.find_first_of("aeiou"); // first vowel
s1.find_last_of("aeiou"); // last vowel
s1.find_first_not_of("abc"); // first not abc
7. Substrings
-------------
string s2 = [Link](2); // from index 2
Page 1
C++ std::string Functions Cheat Sheet
string s3 = [Link](2, 4); // length 4
8. Comparison
-------------
if (s1 == s2) { }
if ([Link](s2) == 0) { }
9. Conversion
-------------
string numStr = "123";
int x = stoi(numStr);
long y = stol(numStr);
double d = stod("3.14");
int num = 456;
string s = to_string(num);
10. Iteration
-------------
for (char c : s1) cout << c << " ";
for (int i = 0; i < [Link](); i++) cout << s1[i];
Tip: Use ios::sync_with_stdio(false); [Link](nullptr); for speed.
Page 2