<algorithm> is the C++ standard library header for general-purpose operations on container elements and ranges. It separates algorithms from data storage, so the same operation can work with different containers. section 2.0
Common uses include:
- Check a sequence with std::all_of, std::any_of, or std::none_of; each accepts a unary predicate such as a lambda. section 2.3.4
- Generate values across an array with std::generate or std::ranges::generate. section 5.1.6
- Shuffle elements with std::shuffle or std::ranges::shuffle and a random number generator. section 5.2.1
- Remove or select elements with algorithms such as std::remove_if, then use ranges and views when direct iterator handling is unnecessary. section 2.3.4
Checking a row of numbers
A range-based check keeps the intent visible: assert(std::ranges::all_of(row, [](auto x) { return x >= 0; })); The lambda supplies the condition, while the ranges overload accepts the container directly. section 2.3.4
assert(std::ranges::all_of(row, [](auto x) { return x >= 0; }));
Inspect the compact ranges-based all_of example and its lambda predicate. section 2.3.4
