Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Ranges and views in C++

Concept page on C++20/23 ranges: range concepts, lazy views, the pipe operator, range adaptors, projections, ranges::to and dangling iterators.

A range is anything you can iterate with std::ranges::begin and std::ranges::end: a container, a C array, a std::string_view, a pair of iterators wrapped in std::ranges::subrange, or a generated sequence. C++20 turned this idea into a set of concepts and added a library of algorithms and views that work on any range, replacing the old begin(), end() pairs with a single argument.

A view is a cheap-to-copy, non-owning, lazy range. It describes a transformation (filter, transform, take, reverse...) and does the work only when you iterate it. Views compose with the pipe operator |, which makes data-processing code read from left to right. C++23 added std::ranges::to to turn a pipeline back into a container, plus more views such as zip, enumerate, chunk, slide, join_with and cartesian_product.

C++
auto r = v | std::views::filter(is_even) | std::views::transform(square);

Try it

C++ A lazy pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Range algorithms, projections and ranges::to

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Concepts

The hierarchy mirrors the iterator categories:

Concept Requirement Examples
std::ranges::range Has begin and end. everything below
input_range Single-pass reading. istream_view
forward_range Multi-pass. std::forward_list
bidirectional_range Can step backwards. std::list, std::map
random_access_range Constant-time jumping. std::deque
contiguous_range Elements adjacent in memory. std::vector, std::array, C arrays
sized_range std::ranges::size is constant time. std::vector
view Cheap to copy and move. views::filter, std::string_view, std::span
borrowed_range Iterators stay valid after the range object is gone. std::string_view, std::span, subrange

Common views and adaptors

views::all, filter, transform, take, take_while, drop, drop_while, reverse, iota, keys, values, elements<N>, split, join, common, counted, empty, single, repeat (C++23), zip, zip_transform, enumerate, chunk, slide, stride, join_with, cartesian_product, adjacent, pairwise (C++23).

Usage notes

  • Views are lazy and re-evaluated each pass. An expensive transform runs again on every iteration; materialize with ranges::to when you iterate repeatedly.
  • Views do not own elements. The underlying container must outlive them. Passing an rvalue container to an adaptor (std::vector{1, 2} | views::reverse) makes the view take ownership through owning_view, but a view built from an lvalue container still refers to it.
  • Dangling protection: range algorithms that return iterators return std::ranges::dangling when given a temporary non-borrowed range, so you get a compile error instead of a bug.
  • Mutation can break views: changing the source (inserting into a vector) invalidates iterators of any view over it. filter also caches its first match, so it can behave unexpectedly if you change elements it tests after the first iteration.
  • Const iteration: some views (filter, drop_while) are not const-iterable. Take them by forwarding reference (auto&&) in generic code.
  • Projections (std::ranges::sort(v, {}, &T::member)) apply a function to each element before comparing, which removes most hand-written lambdas.
  • Constrained algorithms in std::ranges also give shorter error messages than the old std:: overloads when the concept is not satisfied.
  • Pipelines versus loops: a pipeline is expressive but may compile slower and be harder to step through in a debugger. A plain for is fine when it is clearer.
  • Mutating through a view: transform with a function that returns by value produces prvalues, so you cannot assign through it, but views::filter, take and reverse over a non-const container yield references you can modify.