Ranges Views: Lazy, Non-Mutating Iterations with Materialization
Explore how C++ ranges views provide lazy, non-mutating iterations over data without owning elements. Understand view construction with O(1) complexity, how views act as proxies transforming data lazily, and how to materialize views into containers like vectors. Gain insights into when sorting views is possible and why materialization is often required, enhancing your ability to write efficient and clear C++ range-based code.
Views are non-owning ranges with complexity guarantees
Any type that provides the functions begin() and end(), where begin() returns an iterator and end() returns a sentinel, qualifies as a range. We concluded that all standard containers are ranges. Containers own their elements, so we can, therefore, call them owning ranges.
A view is also a range, that is, it provides begin() and end() functions. However, unlike containers, a view does not own the elements in the range that the view spans over.
The construction of a view is required to be a constant-time operation, . It cannot perform any work that depends on the size of the underlying container. The same goes for assigning, copying, moving, and destructing a view. This makes it easy to reason about performance when using views to combine multiple algorithms. ...