C++ by Example
STL Algorithms
2 min read
STL Algorithms
<algorithm> provides over 100 generic algorithms that work on any container via iterators. They cover sorting, searching, transforming, partitioning, and more — all without the container needing to know about the algorithm, and vice versa.
Sorting
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {5, 2, 8, 1, 9, 3};
std::sort(v.begin(), v.end()); // ascending
std::sort(v.begin(), v.end(), std::greater<>()); // descending
std::stable_sort(v.begin(), v.end()); // preserves relative order of equal elements
bool sorted = std::is_sorted(v.begin(), v.end());
std::cout << sorted << "\n"; // 1
}
Searching
std::vector<int> v = {1, 2, 3, 4, 5, 6};
// Linear search — O(n)
auto it = std::find(v.begin(), v.end(), 4);
if (it != v.end()) std::cout << "found: " << *it << "\n";
// Binary search — O(log n), requires sorted range
bool found = std::binary_search(v.begin(), v.end(), 4);
auto lb = std::lower_bound(v.begin(), v.end(), 4); // first element >= 4
auto ub = std::upper_bound(v.begin(), v.end(), 4); // first element > 4
// Find with predicate
auto pos = std::find_if(v.begin(), v.end(), [](int x) { return x > 3; });
std::cout << *pos << "\n"; // 4
Transforming
#include <algorithm>
#include <vector>
#include <numeric>
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> squares(v.size());
std::transform(v.begin(), v.end(), squares.begin(),
[](int x) { return x * x; });
// squares: {1, 4, 9, 16, 25}
// In-place transform
std::transform(v.begin(), v.end(), v.begin(), [](int x) { return x * 2; });
// v: {2, 4, 6, 8, 10}
Numeric algorithms
#include <numeric>
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::accumulate(v.begin(), v.end(), 0); // 15
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<>()); // 120
std::vector<int> running(v.size());
std::partial_sum(v.begin(), v.end(), running.begin());
// running: {1, 3, 6, 10, 15}
std::vector<int> diffs(v.size());
std::adjacent_difference(v.begin(), v.end(), diffs.begin());
// diffs: {1, 1, 1, 1, 1}
Partitioning and removing
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
// Partition: evens before odds
auto mid = std::partition(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
// Remove-erase idiom
v = {1, 2, 3, 2, 4, 2, 5};
v.erase(std::remove(v.begin(), v.end(), 2), v.end());
// v: {1, 3, 4, 5}
std::remove does not actually erase elements — it moves non-removed elements to the front and returns an iterator to the new end. The .erase() call does the actual removal.
Copying and filling
std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dst(5);
std::copy(src.begin(), src.end(), dst.begin());
std::vector<int> evens;
std::copy_if(src.begin(), src.end(), std::back_inserter(evens),
[](int x) { return x % 2 == 0; });
// evens: {2, 4}
std::fill(dst.begin(), dst.end(), 0);
std::fill_n(dst.begin(), 3, 99); // first 3 elements = 99
Min, max, and counting
auto [mn, mx] = std::minmax_element(v.begin(), v.end());
int cnt = std::count(v.begin(), v.end(), 2);
int cnt_if = std::count_if(v.begin(), v.end(), [](int x){ return x > 3; });
bool any = std::any_of(v.begin(), v.end(), [](int x){ return x > 4; });
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; });
C++ by Example
Learn modern C++ through working code. Each chapter introduces one concept — variables, functions, classes, templates, smart pointers, concurrency — with a clear example, a line-by-line explanation, and notes on how it applies in real programs.
View book →