Back to results for “Ranges & Views”
C++ › Ranges & Views

What is a view in the ranges library?

Beginner

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.

Common follow-ups: Why are views lazy? | Do views own their elements?