liveBook · Manning

<algorithm>

As explained in Learn C++ by Example

<algorithm> is the C++ standard library header that declares general-purpose algorithms for operating on sequences and ranges.

<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

How Learn C++ by Example covers it

How deep it goes
Explains predicates, lambdas, ranges, iterators, and selected algorithm behavior through working examples, but does not catalog the entire header.
Examples
Demonstrates Pascal’s triangle validation, removal and filtering, card-deck generation, and card shuffling.
What it assumes
Assumes basic C++ containers and functions while reinforcing vectors, lambdas, iterators, ranges, and predicates through examples.
What you can do after
Apply standard and ranges algorithms to query, generate, filter, remove, and shuffle container elements.
How it connects
Connects algorithms with containers, iterators, ranges, views, lambdas, scoped enumerations, and random number generators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.