C++C++ Explainer
C++ · 5 · STL21 / 36 · 58%

Containers & Iterators

vector, map, unordered_map — pick the right container for the access pattern.

shortcuts: ← prev · → next · M mark
1

vector

Dynamic array. O(1) push_back amortised, O(1) random access.

Example
example
#include <vector>
std::vector<int> v{1, 2, 3};
v.push_back(4);
for (int x : v) std::cout << x << ' ';
cap 2cap 4cap 8push_back reallocates + copies when size > cap
doubling capacity
2

map vs unordered_map

map = ordered tree (O(log n)); unordered_map = hash table (O(1) avg).

Example
example
#include <unordered_map>
std::unordered_map<std::string, int> ages;
ages["Ada"] = 36;
ages["Ken"] = 70;
for (auto& [name, age] : ages)
    std::cout << name << '=' << age << '\n';
3

Algorithms

The <algorithm> header operates on iterator ranges.

Example
example
#include <algorithm>
std::vector<int> v{4, 1, 3, 2};
std::sort(v.begin(), v.end());
auto it = std::find(v.begin(), v.end(), 3);
4

C++20 Ranges

Composable, lazy, no iterator boilerplate.

Example
example
#include <ranges>
#include <vector>

std::vector v{1, 2, 3, 4, 5};
auto evens_sq = v
    | std::views::filter([](int x){ return x % 2 == 0; })
    | std::views::transform([](int x){ return x * x; });
for (int x : evens_sq) std::cout << x << ' ';  // 4 16