Create views into containers with ranges
The new ranges
library is one of the more significant additions to C++20. It provides a new paradigm for filtering and processing containers. Ranges provide clean and intuitive building blocks for more effective and readable code.
Let's start by defining a few terms:
- A Range is a collection of objects which can be iterated. In other words, any structure that supports the
begin()
andend()
iterators is a range. This includes most STL containers. - A View is a range that transforms another underlying range. Views are lazy, meaning they only operate as the range iterates. A view returns data from the underlying range and does not own any data itself. Views operate in O(1) constant time.
- A View Adapter is an object that takes a range and returns a view object. A view adapter may be chained with other view adapters using the
|
operator.Note
The
<ranges>
library uses thestd::ranges
and thestd::ranges::view
namespaces....