Over 100 algorithms that work on any range of iterators. Combined with lambdas, this header eliminates most hand-written loops.
Include: #include <algorithm> | Namespace: std | Compile: g++ -Wall -std=c++17 ...
| Name | Type / Signature | Description |
|---|---|---|
| std::sort | void sort(first, last [, cmp]) | Sort range ascending (O(n log n)) |
| std::stable_sort | void stable_sort(first, last [, cmp]) | Stable sort — preserves equal element order |
| std::find | It find(first, last, val) | Iterator to first element == val |
| std::find_if | It find_if(first, last, pred) | Iterator to first element satisfying pred |
| std::binary_search | bool binary_search(first, last, val) | True if val exists in sorted range |
| std::lower_bound | It lower_bound(first, last, val) | Iterator to first element >= val |
| std::upper_bound | It upper_bound(first, last, val) | Iterator to first element > val |
| std::count | ptrdiff_t count(first, last, val) | Count occurrences of val |
| std::count_if | ptrdiff_t count_if(first, last, pred) | Count elements satisfying pred |
| std::transform | Out transform(first, last, out, op) | Apply op to each element |
| std::for_each | Fn for_each(first, last, fn) | Apply fn to every element |
| std::copy | Out copy(first, last, out) | Copy range to out |
| std::fill | void fill(first, last, val) | Fill range with val |
| std::reverse | void reverse(first, last) | Reverse range in-place |
| std::unique | It unique(first, last) | Remove consecutive duplicates |
| std::remove | It remove(first, last, val) | Move non-val elements forward |
| std::remove_if | It remove_if(first, last, pred) | Remove elements satisfying pred |
| std::max_element | It max_element(first, last) | Iterator to maximum element |
| std::min_element | It min_element(first, last) | Iterator to minimum element |
| std::shuffle | void shuffle(first, last, rng) | Randomly shuffle range (C++11) |
| std::any_of | bool any_of(first, last, pred) | True if any element satisfies pred |
| std::all_of | bool all_of(first, last, pred) | True if ALL elements satisfy pred |
| std::none_of | bool none_of(first, last, pred) | True if NO element satisfies pred |
| std::clamp | const T& clamp(val, lo, hi) | Clamp val to [lo, hi] (C++17) |
Copy, save as demo.cpp, and compile with the command at the bottom.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
using namespace std;
int main() {
vector<int> v = {4, 2, 7, 1, 9, 3, 6, 5, 8};
sort(v.begin(), v.end());
cout << "Sorted: ";
for (int x : v) cout << x << " ";
cout << "\n";
cout << "Has 7? " << (binary_search(v.begin(), v.end(), 7) ? "yes" : "no") << "\n";
cout << "Count >5: " << count_if(v.begin(), v.end(), [](int x){ return x > 5; }) << "\n";
cout << "Sum: " << accumulate(v.begin(), v.end(), 0) << "\n";
vector<int> sq(v.size());
transform(v.begin(), v.end(), sq.begin(), [](int x){ return x * x; });
cout << "Squares: ";
for (int x : sq) cout << x << " ";
cout << "\n";
mt19937 rng(42);
shuffle(v.begin(), v.end(), rng);
cout << "Shuffled: ";
for (int x : v) cout << x << " ";
cout << "\n";
return 0;
}
// Compile: g++ -Wall -std=c++17 -o algo_demo algo_demo.cpp