Back to results for “STL”
C++ › Ranges & Views

Which header provides ranges and how do you use range algorithms?

Beginner

Answer

Ranges live in <ranges> (views/adaptors) and <algorithm> (the std::ranges:: algorithm overloads). You call algorithms in the std::ranges namespace passing a whole range, and build view pipelines with std::views::. Support requires a C++20-capable compiler and standard library.
#include <ranges>
#include <algorithm>
std::ranges::for_each(v, [](int x){ /*...*/ });
Real-world example Adopting <ranges> lets the team write std::ranges algorithms and view pipelines across the codebase.

Common follow-ups: Which headers are needed? | What namespace holds the algorithms?