0% found this document useful (0 votes)
88 views2 pages

C++ std::string Functions Guide

This cheat sheet provides a comprehensive overview of C++ std::string functions, covering creation, input/output, size, character access, modification, searching, substrings, comparison, conversion, and iteration. Each section includes examples demonstrating how to use the various functions effectively. It also includes a performance tip for optimizing input/output operations.

Uploaded by

shinigamiryuk588
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views2 pages

C++ std::string Functions Guide

This cheat sheet provides a comprehensive overview of C++ std::string functions, covering creation, input/output, size, character access, modification, searching, substrings, comparison, conversion, and iteration. Each section includes examples demonstrating how to use the various functions effectively. It also includes a performance tip for optimizing input/output operations.

Uploaded by

shinigamiryuk588
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like