Advertisement
Beginner C++, Ranges & Views

What are C++20 ranges?

Short answer

Ranges (C++20) are an abstraction over 'something you can iterate'—anything with a begin and end—paired with algorithms that operate on whole ranges instead of iterator pairs.

Save C++ mock interview
Advertisement
AModel answer
Ranges (C++20) are an abstraction over 'something you can iterate'—anything with a begin and end—paired with algorithms that operate on whole ranges instead of iterator pairs. The std::ranges algorithms (std::ranges::sort(v)) are cleaner and safer than passing v.begin(), v.end(), and they enable composable views.
std::vector<int> v{3,1,2};
std::ranges::sort(v);   // no begin()/end() needed
Real-world example
std::ranges::sort(v) replaces std::sort(v.begin(), v.end()), removing a common iterator-mismatch bug.
Likely follow-ups
  • How do range algorithms differ from classic ones? | What is a range?
Related topics