Algorithms in C++
From the C++ cheat sheet · Algorithms & Iterators · verified Jul 2026
Algorithms
Sort, search, transform, and accumulate.
cpp
// Quick Reference
#include <algorithm>
std::sort(v.begin(), v.end());
std::find(v.begin(), v.end(), x);
auto n = std::count_if(v.begin(), v.end(), pred);💡 Most algorithms take a [begin, end) iterator pair - end is one past the last element.
⚡ std::find/find_if return the end iterator when nothing matches - always compare to .end().
📌 std::accumulate lives in <numeric>, not <algorithm>; its 3rd arg is the initial value.
🟢 C++20 ranges let you write std::ranges::sort(v) without begin()/end() (see Modern C++).
algorithmsiterators