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::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 ?

9

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
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest