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.

7.1 Introducing ranges

7.2 Creating read-only views over data

7.2.1 Filter function for ranges

7.2.2 Transform function for ranges

7.2.3 Lazy evaluation of range values

7.3 Mutating values through ranges

7.4 Using delimited and infinite ranges

7.4.1 Using delimited ranges to optimize handling input ranges

7.4.2 Creating infinite ranges with sentinels

7.5 Using ranges to calculate word frequencies

Summary

sitemap