cppreference states:
The ranges library is an extension and generalization of the algorithms and iterator libraries that makes them more powerful by making them composable and less error-prone.
The library creates and manipulates range views, lightweight objects that indirectly represent iterable sequences (ranges).
It mentions using range views, which are, as cppreference states:
The range concept defines the requirements of a type that allows iteration over its elements by providing an iterator and sentinel that denote the elements of the range.
But from an outside perspective, it just seems like a wrapper of an iterator with a concept
. So the main question is:
- What are the problems with using regular algorithms that the ranges library solves (code examples will be appreciated), and when should you use it?
return std::forward<R>(r) | std::views::take(3) | std::views::reverse;
,for (int i : ints | std::views::filter(even) | std::views::transform(square))
. But obviously since range is just a pair of iterators you can always get the same result with regular functions taking a pair of iterators. – Amorphousoperator <
is enough for iterator version, ranges version might require totally_ordered concept (so <, >, <=, >=, ==, !=)) – Scammony