Ranges (C++20)
Ranges provide composable, lazy operations over sequences. You can build pipelines with views and then consume them efficiently.
Pipeline Example
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> data{1, 2, 3, 4, 5, 6};
auto result = data
| std::views::filter([](int n) { return n % 2 == 0; })
| std::views::transform([](int n) { return n * 10; });
for (int n : result) {
std::cout << n << " ";
}
}
Common Views
std::views::filterstd::views::transformstd::views::takestd::views::drop
Benefits
- Readable data-flow style
- Lazy evaluation
- Fewer temporary containers
[!TIP] Prefer ranges for transformation pipelines; use classic loops when mutation-heavy control flow is clearer.