Our book, Learning C++, has aWhen ranges are used, it makes coding easier and simper to develop and to understand, even though any code that uses range, can be also coded with an old style for..loop.
Here is an example:
#include <iostream>
#include <vector>
#include <ranges>
#include <numeric>
int main()
{
std::vector<int> nums = { 0, 1, 2, 3, 4, 5 };
auto even = [](int i) { return i % 2 == 0; };
auto square = [](int i) { return i * i; };
auto result = std::ranges::views::filter(nums, even)
| std::ranges::views::transform(square)
| std::ranges::views::all;
int sum = std::accumulate(result.begin(), result.end(), 0);
std::cout << "The sum of squared even numbers is: " << sum << std::endl;
return 0;
}
So what do we have here? our code snippet demonstrates the use of range-based algorithms in C++ 20. In our code, std::ranges::views::filter algorithm takes the vector nums and applies the lambda function even to it to filter out only the even integers. The std::ranges::views::transform algorithm then applies the lambda function square to each element of the filtered range to square them. The resulting range is then converted into a view that includes all the elements using std::ranges::views::all.
The std::accumulate algorithm then takes the resulting view and computes the sum of all the elements in it. Finally, the sum is then printed.
You can read more about ranges in our book.

As you can see, ranges provide a more concise and expressive way to manipulate sequences, using a pipeline-like syntax. The pipeline-like syntax allows you to chain together multiple operations that operate on a sequence of elements, creating a sequence of intermediate results until a final result is produced.
Further more, ranges also allow for the creation of views. A view is a lightweight object that represents a view of a sequence of elements, without actually copying the elements themselves. Views allow you to create lazy evaluation, meaning that the elements are evaluated only when needed, which can save time and memory.
One of the key benefits of ranges is that they simplify code, making it easier to read, write, and understand. They provide a more expressive syntax than the traditional approach of writing for-loops and calling algorithms. Ranges also provide compile-time safety, reducing the risk of errors and making it easier to debug code.
Another advantage of ranges is that they can improve performance. By creating views, it’s possible to defer the computation of elements until they are actually needed, which can reduce the number of intermediate results and the overall memory usage. Ranges also make it easier to write code that can take advantage of parallelism.