7 Ranges
This chapter covers
- The problems of passing iterator pairs to algorithms
- What ranges are and how to use them
- Creating chained range transformations using the pipe syntax
- Understanding range views and actions
- Writing succulent code without
for
loops
In chapter 2, you saw why you should avoid writing raw for
loops and that you should instead rely on using generic algorithms provided to you by the STL. Although this approach has significant benefits, you’ve also seen its downsides. The algorithms in the standard library were not designed to be easily composed with each other. Instead, they’re mostly focused on providing a way to allow implementation of a more advanced version of an algorithm by applying one algorithm multiple times.
A perfect example is std::partition
, which moves all items in a collection that satisfy a predicate to the beginning of the collection, and returns an iterator to the first element in the resulting collection that doesn’t satisfy the predicate. This allows you to create a function that does multigroup partitioning—not limited to predicates that return true
or false
—by invoking std::partition
multiple times.