Topic 9 of 27 · Advanced
Functions Reference: STL & string
std::string methods, container member functions and <algorithm> functions.
Concept
The C++ standard library gives you member functions on containers (push_back, insert, find) and free algorithms in <algorithm> and <numeric> that work on any iterator range.
Most algorithms take a range [begin, end) and many accept a lambda to customise behaviour.
Knowing these by heart is a big speed boost in coding rounds and competitive programming.
Cheat sheet
| Function | What it does | Example |
|---|---|---|
| s.size() / s.empty() | Length / is empty | s.size() |
| s.substr(pos, len) | Part of a string | "placement".substr(0, 5) → "place" |
| s.find(x) / s.rfind(x) | Position or string::npos | s.find("@") |
| s.push_back(c) / s += t | Append | s += "!" |
| s.insert / s.erase / s.replace | Edit in place | s.erase(0, 1) |
| std::to_string(n) | Number → string | to_string(42) → "42" |
| std::stoi / stoll / stod | String → number | stoi("42") → 42 |
| getline(cin, s) | Read a whole line | getline(cin, line) |
| v.push_back / emplace_back | Add to vector end | v.emplace_back(1, 2) |
| v.pop_back / back / front | Remove / read ends | v.back() |
| v.insert(it, x) / v.erase(it) | Insert / remove at position | v.erase(v.begin()) |
| v.reserve(n) / v.resize(n) | Capacity / size | v.reserve(1000) |
| m[key] / m.at(key) | Access (at throws if missing) | m.at("TCS") |
| m.find(k) / m.count(k) / m.contains(k) | Lookup | if (m.contains(k)) |
| m.insert / m.emplace / m.erase | Add / remove | m.erase(k) |
| set.lower_bound(x) | First element ≥ x in a set | s.lower_bound(5) |
| pq.push / pq.top / pq.pop | Heap operations | pq.top() |
| sort(b, e, cmp) | Sort (O(n log n)) | sort(v.begin(), v.end()) |
| stable_sort | Sort keeping equal-item order | stable_sort(...) |
| reverse(b, e) | Reverse a range | reverse(s.begin(), s.end()) |
| min_element / max_element | Iterator to min / max | *max_element(v.begin(), v.end()) |
| find / find_if | First match | find_if(b, e, [](int x){return x>5;}) |
| count / count_if | Count matches | count(s.begin(), s.end(), 'a') |
| binary_search | Is value present (sorted) | binary_search(b, e, 7) |
| lower_bound / upper_bound | First ≥ / first > (sorted) | lower_bound(b, e, x) - b |
| accumulate (numeric) | Sum / fold | accumulate(b, e, 0LL) |
| partial_sum (numeric) | Prefix sums | partial_sum(b, e, out) |
| iota (numeric) | Fill 0, 1, 2, ... | iota(v.begin(), v.end(), 0) |
| transform | Apply a function to each | transform(b, e, b, ::toupper) |
| unique + erase | Remove adjacent duplicates | v.erase(unique(b, e), e) |
| fill / fill_n | Set all to a value | fill(dp.begin(), dp.end(), -1) |
| swap / max / min | Swap / compare two values | max({a, b, c}) |
| next_permutation | Next arrangement | do {...} while (next_permutation(b, e)) |
| rotate(b, mid, e) | Rotate a range | rotate(b, b + k, e) |
| __gcd / std::gcd / std::lcm | GCD / LCM | gcd(12, 18) → 6 |
| __builtin_popcount(x) | Count set bits (GCC) | __builtin_popcount(7) → 3 |
Code (C++20)
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Hello World";
transform(s.begin(), s.end(), s.begin(), ::tolower);
cout << s.substr(0, 5) << ' ' << count(s.begin(), s.end(), 'o') << '\n'; // hello 2
vector<int> v(5);
iota(v.begin(), v.end(), 1); // 1 2 3 4 5
cout << accumulate(v.begin(), v.end(), 0) << '\n'; // 15
cout << *max_element(v.begin(), v.end()) << '\n'; // 5
vector<int> pre(v.size());
partial_sum(v.begin(), v.end(), pre.begin()); // 1 3 6 10 15
int idx = lower_bound(pre.begin(), pre.end(), 6) - pre.begin(); // 2
map<string, int> ctc{{"TCS", 7}};
if (ctc.contains("TCS")) cout << ctc.at("TCS") << ' ' << stoi("42") + idx << '\n';
cout << gcd(12, 18) << ' ' << __builtin_popcount(13) << '\n'; // 6 3
}Remember
- Algorithms work on ranges [begin, end).
- m[key] inserts missing keys; m.at() throws; contains() just checks.
- accumulate with 0LL to avoid int overflow.
Interview questions
string::find returns what when nothing is found?
std::string::npos, the largest size_t value — always compare against npos, not -1 stored in an int.
How do you remove all occurrences of x from a vector?
v.erase(remove(v.begin(), v.end(), x), v.end()), or std::erase(v, x) in C++20.
Lesson test
Take an 8-question test on “Functions Reference: STL & string” — concepts, output prediction, bug spotting and interview questions.