C++advancedcpp20

Ranges and views

Lazy, composable pipelines that replace nested loops and temporary vectors.

The C++20 <ranges> library turns filter, transform, and take into pipe-composable adaptors, and lets std::ranges algorithms take a whole range instead of two iterators.

std::ranges::sort over a whole range

C++intro
0ms
1 2 3 5 8 91 2 3 -4 -7
Reference · output compiled offline, not runnable in-browser

The first line is the plain ascending sort; the second sorts by the projection std::abs(x), so -4 and -7 land last by magnitude (keys 4 and 7) even though they are the smallest by value. The projection replaces a hand-written comparator while still sorting the original elements.