Skip to content
C++

STL Algorithms Cheat Sheet

"Quick reference for the most commonly used STL algorithms: sort, find, transform, accumulate, ranges. With C++20 ranges equivalents."

Headers

cpp
#include <algorithm>   // most algorithms
#include <numeric>     // accumulate, iota, partial_sum, inner_product
#include <ranges>      // std::ranges::* and std::views::* (C++20)

Sorting

cpp
std::sort(v.begin(), v.end());                         // ascending
std::sort(v.begin(), v.end(), std::greater<>{});        // descending
std::sort(v.begin(), v.end(), [](auto& a, auto& b) {
    return a.name < b.name;
});

std::stable_sort(v.begin(), v.end());   // preserves equal element order

std::partial_sort(v.begin(), v.begin()+3, v.end());   // top 3 sorted
std::nth_element(v.begin(), v.begin()+n, v.end());    // partition at n

// C++20 ranges
std::ranges::sort(v);
std::ranges::sort(v, std::greater{});
std::ranges::sort(v, {}, &Person::name);  // sort by member (projection)

Searching

cpp
// Linear search
auto it = std::find(v.begin(), v.end(), 42);
auto it = std::find_if(v.begin(), v.end(), [](int x){ return x > 10; });

bool has = std::any_of(v.begin(), v.end(), [](int x){ return x > 10; });
bool all = std::all_of(v.begin(), v.end(), [](int x){ return x > 0; });
bool none = std::none_of(v.begin(), v.end(), [](int x){ return x < 0; });

// Binary search (requires sorted range)
bool found = std::binary_search(v.begin(), v.end(), 42);
auto lo = std::lower_bound(v.begin(), v.end(), 42);  // first >= 42
auto hi = std::upper_bound(v.begin(), v.end(), 42);  // first > 42

// Count
auto n = std::count(v.begin(), v.end(), 42);
auto n = std::count_if(v.begin(), v.end(), [](int x){ return x%2==0; });

// Min / max
auto it  = std::min_element(v.begin(), v.end());
auto it  = std::max_element(v.begin(), v.end());
auto [mn, mx] = std::minmax_element(v.begin(), v.end());

Transformation

cpp
// transform — apply function to each element
std::transform(v.begin(), v.end(), out.begin(), [](int x){ return x*2; });

// transform into same range (in-place)
std::transform(v.begin(), v.end(), v.begin(), [](int x){ return x*2; });

// transform binary (combine two ranges)
std::transform(a.begin(), a.end(), b.begin(), out.begin(),
               [](int x, int y){ return x + y; });

// for_each — side effects, no output
std::for_each(v.begin(), v.end(), [](int& x){ x *= 2; });

Aggregation

cpp
// accumulate — left fold with binary op
int sum = std::accumulate(v.begin(), v.end(), 0);
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>{});

// reduce (C++17) — like accumulate but order not guaranteed
int sum = std::reduce(v.begin(), v.end(), 0);

// inner product (dot product)
int dot = std::inner_product(a.begin(), a.end(), b.begin(), 0);

Copy / fill / remove

cpp
std::copy(src.begin(), src.end(), dst.begin());
std::copy_if(src.begin(), src.end(), std::back_inserter(dst),
             [](int x){ return x > 0; });

std::fill(v.begin(), v.end(), 0);
std::iota(v.begin(), v.end(), 1);   // 1, 2, 3, 4, ...

// Remove (does not resize — erase-remove idiom)
auto end = std::remove(v.begin(), v.end(), 42);
v.erase(end, v.end());

// C++20: std::erase and std::erase_if (no idiom needed)
std::erase(v, 42);
std::erase_if(v, [](int x){ return x%2==0; });

Permutations

cpp
std::reverse(v.begin(), v.end());
std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
std::rotate(v.begin(), v.begin()+2, v.end());  // shift left by 2
std::next_permutation(v.begin(), v.end());

C++20 Ranges views

cpp
namespace views = std::views;

auto evens = v | views::filter([](int x){ return x%2==0; });
auto doubled = v | views::transform([](int x){ return x*2; });
auto first5 = v | views::take(5);
auto skip3  = v | views::drop(3);

// Chain
auto result = v | views::filter([](int x){ return x > 0; })
                | views::transform([](int x){ return x*2; })
                | views::take(10);

// Lazy — nothing computed until iterated
for (int x : result) { ... }

// Materialize to vector
auto vec = result | std::ranges::to<std::vector>();   // C++23
Edit on GitHub