<algorithm>
<algorithm> is the standard header of iterator-based algorithms: find, sort, copy, count, transform, and dozens more. You pass [first, last) iterators and often a predicate or output iterator. C++20 adds the same names under std::ranges.
Detailed Explanation
The STL's bet was that if every container exposes iterators, one std::find can serve all of them. <algorithm> is that bet. You include it, you pass a pair of iterators, and you get a well-specified, usually well-optimised loop you did not write.
The algorithms fall into groups:
- Non-modifying.
find,find_if,count,all_of,any_of,none_of,equal,mismatch,search. - Modifying.
copy,copy_if,move,fill,transform,replace,remove/remove_if(which do not shorten the container; see erase-remove),reverse,rotate,unique. - Sorting and partitioning.
sort,stable_sort,partial_sort,nth_element,partition,is_sorted. - Binary search on a sorted range.
lower_bound,upper_bound,equal_range,binary_search. - Heaps.
make_heap,push_heap,pop_heap,sort_heap. - Min/max.
min,max,minmax,min_element. - Permutations.
next_permutation.
Most take a comparator or a unary predicate as the last argument. Pass a lambda. Do not write a loop that is already an algorithm name.
std::remove is the trap. It slides kept elements forward and returns the new logical end. The container's size() is unchanged. You must v.erase(std::remove_if(...), v.end()), or use std::erase_if(v, pred) (C++20) which does both.
Output algorithms write through an output iterator. std::copy(src.begin(), src.end(), std::back_inserter(dst)) grows dst. std::transform can write into the same range if the operations do not overlap badly; prefer a clear destination.
<numeric> is the sibling header: accumulate, inner_product, partial_sum, iota, gcd. <memory> has uninitialized_*. C++20 <ranges> re-exports many algorithms as std::ranges::find(v, x) with projections.
Execution policies (std::execution::par) exist in <execution> for some algorithms. They are optional, not free parallelism, and they change exception and iterator-invalidation assumptions. Do not sprinkle par without measuring and without a thread-safe predicate.
Write a raw loop when the body is the business logic (parsing a line, talking to a socket). Use <algorithm> when the body is "find", "count", "copy", "sort". The name documents the intent and the implementation is already reviewed.
Code Examples
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{3, 1, 4, 1, 5};
auto it = std::find(v.begin(), v.end(), 4);
std::cout << (it - v.begin()) << '\n'; // prints: 2
std::sort(v.begin(), v.end());
std::cout << std::count(v.begin(), v.end(), 1) << '\n'; // prints: 2
}#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 2, 3, 4, 5};
std::erase_if(v, [](int n) { return n % 2 == 0; }); // C++20
for (int n : v) std::cout << n << ' ';
std::cout << '\n'; // prints: 1 3 5
}Common Gotchas
`remove` does not erase
forgetting `v.erase(..., v.end())` leaves garbage at the tail. Use `erase_if` on C++20.
Mismatched iterator pairs
`std::find(a.begin(), b.end(), x)` is undefined. `std::ranges::find(a, x)` cannot make that mistake.
Predicates should be pure
`sort` with a comparator that mutates elements, or that is not a strict weak order, is undefined behaviour.
Parallel policies and throwing predicates
an exception in a `par` algorithm can call `terminate`. Keep predicates `noexcept` if you parallelise.