C++20 Ranges and Sorting
I'm Dealing with the Last Big 4 of C++ 20, Attempting to Learn the New Main Features. Trying Some Code from the Web Related to Ranges, I've Written: Std...
I'm dealing with the last big 4 of C++ 20, attempting to learn the new main features. Trying some code from the web related to ranges, I've written:
std::vector ints{ 6, 5, 2, 8 };
auto even = [](int i) {
return 0 == i % 2;
};
// ranges...
auto rr = ints | std::views::filter(even)
| std::views::transform([](auto i) {
return i * i;
})
| std::views::reverse;
Then I would sort, like range-v3 does with |action::sort, but I've understand that this implementation is not the same.
The way I've found to sort is:
ints = std::vector(std::ranges::begin(rr), std::ranges::end(rr));
std::ranges::sort(ints);
Am I wrong? Does anybody know how to sort with pipe style the view ?
1 Answer
Then I would sort, like range-v3 does with
|action::sort...
No, you can't actually sort rr like this:
rr |= ranges::actions::sort; // error
because rr is a view. While views can provide mutable access to the underlying range, sort additionally needs the range to support random access. A lazily generated view like rr does not allow this.
You can create a vector from rr as you have done, and then you can use actions on that range:
ints |= ranges::actions::sort; // ok
c++20, however, doesn't have any actions (hopefully we'll get them in c++23), so until then you'll have to call the algorithm sort without the pipe syntax:
std::ranges::sort(ints); // ok