<ranges> provides the ranges library (C++20): a new way to express algorithms that works on ranges directly instead of iterator pairs. Instead of std::sort(v.begin(), v.end()), you write std::ranges::sort(v). Range adapters like std::views::filter compose transformations lazily.
It's more expressive and composable than the classic iterator-based algorithms, but requires C++20 or later.
This example sorts a vector and filters even numbers using range-based algorithms and views with the pipe operator.
// compile: g++ -std=c++20 -o rangesexample rangesexample.cpp // run: ./rangesexample // description: ranges library with views #include <ranges> #include <vector> #include <iostream> int main() { std::vector<int> nums = {1, 2, 3, 4, 5}; std::ranges::sort(nums); auto evens = nums | std::views::filter([](int x) { return x % 2 == 0; }); for (int x : evens) { std::cout << x << " "; } std::cout << "\n"; return 0; }