C++20 ranges and sorting

7.6k views Asked by At

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

There are 1 answers

7
cigien On BEST 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