← C++ roadmap

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

FunctionWhat it doesExample
s.size() / s.empty()Length / is emptys.size()
s.substr(pos, len)Part of a string"placement".substr(0, 5) → "place"
s.find(x) / s.rfind(x)Position or string::nposs.find("@")
s.push_back(c) / s += tAppends += "!"
s.insert / s.erase / s.replaceEdit in places.erase(0, 1)
std::to_string(n)Number → stringto_string(42) → "42"
std::stoi / stoll / stodString → numberstoi("42") → 42
getline(cin, s)Read a whole linegetline(cin, line)
v.push_back / emplace_backAdd to vector endv.emplace_back(1, 2)
v.pop_back / back / frontRemove / read endsv.back()
v.insert(it, x) / v.erase(it)Insert / remove at positionv.erase(v.begin())
v.reserve(n) / v.resize(n)Capacity / sizev.reserve(1000)
m[key] / m.at(key)Access (at throws if missing)m.at("TCS")
m.find(k) / m.count(k) / m.contains(k)Lookupif (m.contains(k))
m.insert / m.emplace / m.eraseAdd / removem.erase(k)
set.lower_bound(x)First element ≥ x in a sets.lower_bound(5)
pq.push / pq.top / pq.popHeap operationspq.top()
sort(b, e, cmp)Sort (O(n log n))sort(v.begin(), v.end())
stable_sortSort keeping equal-item orderstable_sort(...)
reverse(b, e)Reverse a rangereverse(s.begin(), s.end())
min_element / max_elementIterator to min / max*max_element(v.begin(), v.end())
find / find_ifFirst matchfind_if(b, e, [](int x){return x>5;})
count / count_ifCount matchescount(s.begin(), s.end(), 'a')
binary_searchIs value present (sorted)binary_search(b, e, 7)
lower_bound / upper_boundFirst ≥ / first > (sorted)lower_bound(b, e, x) - b
accumulate (numeric)Sum / foldaccumulate(b, e, 0LL)
partial_sum (numeric)Prefix sumspartial_sum(b, e, out)
iota (numeric)Fill 0, 1, 2, ...iota(v.begin(), v.end(), 0)
transformApply a function to eachtransform(b, e, b, ::toupper)
unique + eraseRemove adjacent duplicatesv.erase(unique(b, e), e)
fill / fill_nSet all to a valuefill(dp.begin(), dp.end(), -1)
swap / max / minSwap / compare two valuesmax({a, b, c})
next_permutationNext arrangementdo {...} while (next_permutation(b, e))
rotate(b, mid, e)Rotate a rangerotate(b, b + k, e)
__gcd / std::gcd / std::lcmGCD / LCMgcd(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.

Practise C++ with the AI mentor

Lesson test

Take an 8-question test on “Functions Reference: STL & string” — concepts, output prediction, bug spotting and interview questions.

Can't find a C++ topic you need for exams or placements? Request it and we'll add it.