Ranges
C++20 ranges are a layer over iterators: a range is anything with a beginning and an end (or a sentinel). Algorithms take the range in one argument, and views compose lazily (filter | transform) without allocating an intermediate container.
Detailed Explanation
std::sort(v.begin(), v.end()) repeats the container name and lets you pass mismatched iterators. std::ranges::sort(v) takes the container. That is the first, smallest gift of <ranges>. The larger gift is views: adaptors that present a different sequence over the same elements, computed as you iterate.
A range is a type you can call ranges::begin and ranges::end on. Containers are ranges. A span is a range. A raw array is a range. A pair of iterators can be wrapped as a subrange.
A view is a cheap range that does not own its elements (with small exceptions). std::views::filter(v, pred) is a view. Iterating it skips elements that fail pred. Nothing is copied into a new vector unless you ask (ranges::to<vector> in C++23, or a handwritten loop).
Pipelines use |:
for (int n : nums | std::views::filter(even) | std::views::transform(square))
Each adaptor returns a view. Evaluation is lazy: square runs when the for asks for the next element, not when the pipeline is built. That is why a pipeline over a temporary container can dangle: the view holds a reference or iterator into a dead object. Bind the container to a name first.
Common adaptors: filter, transform, take, drop, reverse, keys, values, enumerate (C++23), iota (an infinite or bounded counter). views::all turns a container into a view. views::common forces begin and end to the same type for older APIs.
ranges:: algorithms are the iterator algorithms with extra constraints and projections. ranges::find(users, id, &User::id) finds by member. They do not go through .begin() in a way that allows a mismatched pair.
Not every view is sized or random-access. filter is not sized (you cannot know how many pass without walking). reverse on a bidirectional range is bidirectional. The concept on the view tells you what algorithms accept it.
Ranges do not replace containers. They replace "copy into a temp vector, then another, then loop". Use a container when you need to own, index repeatedly, or persist. Use a view when you need to present or traverse.
C++20 support varies. GCC 10+, Clang 15+, and MSVC 19.29 cover the everyday adaptors. Check your toolchain before you commit a pipeline in a public header.
Code Examples
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> nums{1, 2, 3, 4, 5, 6};
auto even = [](int n) { return n % 2 == 0; };
auto square = [](int n) { return n * n; };
for (int n : nums | std::views::filter(even) | std::views::transform(square)) {
std::cout << n << ' ';
}
std::cout << '\n'; // prints: 4 16 36
}#include <algorithm>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
struct User { std::string name; int id; };
int main() {
std::vector<User> users{{"ada", 1}, {"al", 2}};
auto it = std::ranges::find(users, 2, &User::id);
std::cout << it->name << '\n'; // prints: al
}Common Gotchas
Piping a temporary
`(std::vector<int>{1, 2} | std::views::filter(even))` dangles when the full expression ends. Name the vector.
`filter` is not sized
`ranges::size` will not compile. Do not assume you can `reserve` from the view's size.
Lazy views re-run the predicate
walking a `filter` twice runs `pred` twice. If `pred` is expensive or mutating, materialise a vector.
Older algorithms want a common iterator type
some `begin`/`end` pairs use a sentinel. Wrap with `views::common` or use `std::ranges::` algorithms.