Much of the functionality provided by the various range adaptors seem to be quite similar to the algorithms library.
For example std::ranges::transform_view
A range adaptor that represents view of an underlying sequence after applying a transformation function to each element.
and std::ranges::transform
Applies the given function to a range and stores the result in another range, beginning at result.
If we wanted to turn a string into uppercase we could use both the algorithm and view:
int main() {
std::string in{ "hello\n" };
std::string out;
// using transform view
std::ranges::copy( std::views::transform(in, toupper), std::back_inserter(out) );
std::cout << out;
out.clear();
// using transform algorithm
std::ranges::transform(in, std::back_inserter(out), ::toupper);
std::cout << out;
return 0;
}
What do the views accomplish that the algorithms can't, and vice versa? When should I prefer one over the other?