What is the correct way of filtering the output of group_by with Range-v3?

The following code does not compile because x cannot be compared with a std::vector<int>, as x is a more complex structure handled by Range-v3 (which is good, because it's lazy, unlike std::vector<int>).

How can I write the filtering lambda to cope with that, possibly without forcing an unnecessary conversion of x to std::vector<int>?

#include <range/v3/view/filter.hpp>
#include <range/v3/view/group_by.hpp>
#include <vector>

int main() {
    std::vector<int> v{1,2,3,4,5};
    auto w = v
        | ranges::views::group_by([](auto const& c1, auto const& c2){ return c1 == 3 && c2 == 4; })
        | ranges::views::filter([](auto const& x){ return x != std::vector<int>{3,4}; });

Read more here: https://stackoverflow.com/questions/64940744/what-is-the-correct-way-of-filtering-the-output-of-group-by-with-range-v3

Content Attribution

This content was originally published by Enrico at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: