Range adaptors compose with pipe operator to create views.
1#include <ranges>23std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};45// Single adaptor6auto evens = v | std::views::filter([](int n) { return n % 2 == 0; });78// Composition9auto result = v10 | std::views::filter([](int n) { return n % 2 == 0; })11 | std::views::transform([](int n) { return n * n; })12 | std::views::take(3);13// 4, 16, 361415// Reverse16auto last3 = v | std::views::reverse | std::views::take(3);17// 10, 9, 81819// Chunk20auto chunks = v | std::views::chunk(3);21// [1,2,3], [4,5,6], [7,8,9], [10]2223// Flatten24std::vector<std::vector<int>> nested = {{1,2}, {3,4}, {5,6}};25auto flat = nested | std::views::join;26// 1, 2, 3, 4, 5, 62728// Enumerate (C++23)29for (auto [i, val] : v | std::views::enumerate) {30 std::cout << i << ": " << val << "\n";31}3233// Common view (for legacy APIs)34auto common = v | std::views::filter(pred) | std::views::common;35std::sort(common.begin(), common.end());
Lazy evaluation: