liveBook · Manning

Ranges API

As explained in 100 C++ Mistakes and How to Avoid Them

The C++ ranges API is a standard-library interface for applying algorithms to ranges of elements without requiring explicit iterator pairs.

The C++ ranges API simplifies standard-library algorithms by accepting a container or range directly. Instead of spelling out begin and end iterators, supply the range and the operation to perform. section 4.6

Prefer the ranges form when it makes existing algorithm calls easier to read and maintain:

  • Replace repetitive iterator-pair syntax with the container itself. section 4.6
  • Combine the range with a lambda when the comparison is local and used once. section 4.6
  • Use established Standard Template Library algorithms such as std::sort rather than writing equivalent functions. section 4.6

Sorting students by GPA

A student collection can be sorted with the ranges API and a comparison function, avoiding separate begin and end arguments. The result is described as more compact, readable, and straightforward than the iterator-based alternative. section 4.6

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains direct range passing and readability benefits, but stops short of surveying the API or its implementation.
Examples
Uses student sorting by GPA as the concrete ranges example.
What you can do after
Apply it via the C++ standard library by supplying a container and comparison function instead of iterator pairs.
How it connects
Connects ranges API with lambdas, containers, iterators, and standard-library algorithms such as std::sort.

Also explained in

Compare how 2 books explain ranges API →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.