Search⌘ K

The Concepts Ranges and Views

Explore the C++20 ranges library by learning about concepts such as ranges and views. Understand how ranges represent iterable groups of items and how views apply operations lazily without owning data. This lesson covers various predefined views enabling functional style programming and efficient data transformation.

We'll cover the following...

I already presented the concepts ranges and views when discussing concepts. Consequently, here’s a brief refresher:

  • range: A range is a group of items that you can iterate over. It provides a begin iterator and an end sentinel. The containers of the STL are ranges.

  • view: A view is something that you apply on a range and performs some operation. A view does not own data, and its time complexity to copy, move, or assign is constant.

C++
std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
auto results = numbers | std::views::filter([](int n){ return n % 2 == 0; })
| std::views::transform([](int n){ return n * 2; });

In this code ...