What is a view in the ranges library?
Short answer
A view is a lightweight, non-owning, lazily-evaluated range that adapts another range without copying its elements. Views (like std::views::filter, std::views::transform) compute elements on demand as you iterate, so composing them doesn't create intermediate containers.
Advertisement
AModel answer
A view is a lightweight, non-owning, lazily-evaluated range that adapts another range without copying its elements. Views (like std::views::filter, std::views::transform) compute elements on demand as you iterate, so composing them doesn't create intermediate containers. They're cheap to copy and compose.
auto evens = v | std::views::filter([](int x){ return x%2==0; });Real-world example
A pipeline of views filters and transforms a vector lazily without allocating intermediate vectors.
Likely follow-ups
- Why are views lazy? | Do views own their elements?
Related topics