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 ?
No, you can't actually sort
rr
like this:because
rr
is aview
. Whileview
s can provide mutable access to the underlying range,sort
additionally needs the range to support random access. A lazily generatedview
likerr
does not allow this.You can create a
vector
fromrr
as you have done, and then you can useactions
on that range: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 algorithmsort
without the pipe syntax: