Understanding Range Concepts and Views

Explore the range adaptors and constrained algorithms of the ranges library in this lesson.

The term “range” refers to an abstraction that defines a sequence of elements bounded by start and end iterators. A range, therefore, represents an iterable sequence of elements. However, such a sequence can be defined in several ways:

  • With a begin iterator and an end sentinel. Such a sequence is iterated from the beginning to the end. A sentinel is an object that indicates the end of the sequence. It can have the same type as the iterator type, or it can be of a different type.

  • With a start object and a size (number of elements), representing a counted sequence. Such a sequence is iterated NN times (where NN represents the size) from the start.

  • With a start and a predicate, representing a conditionally terminated sequence. Such a sequence is iterated from the start until the predicate returns false.

  • With only a start value, representing an unbounded sequence. Such a sequence can be iterated indefinitely.

Range concepts in the <ranges> header

All these kinds of iterable sequences are considered ranges. Because a range is an abstraction, the C++20 library defines a series of concepts to describe requirements for range types. These are available in the <ranges> header and the std::ranges namespace. The following table presents the list of range concepts:

Get hands-on with 1200+ tech skills courses.