skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-ranges
This page is read only. You can view the source, but not change it.
# <ranges> **[<ranges>](https://en.cppreference.com/w/cpp/header/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. ```cpp // 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; } ```
Page Tools
Show pagesource
Backlinks
Back to top