Iteratively apply Range-v3 views::filter on a container

  c++, c++20, functional-programming, range-v3

In the for loop following code snippet I’m trying apply views::filter . But it results in type miss match and assignment error.

#include <iostream>
#include <vector>
#include <numeric>
#include <range/v3/all.hpp>

int main()
{
    int limit =100;
    std::vector<int> numbers(100);
    std::iota(numbers.begin(), numbers.end(), 1);
    auto results = numbers | ranges::views::filter([](int n) {return n != 1; });
                           
    int sqrt_limit = std::sqrt(limit);
    for (int i = 2; i <= sqrt_limit; i++)
    {
        results = results | ranges::views::filter([i](int n) {return n == i
            || n % i != 0; });
    }
}

Error C2679 binary '=': no operator found which takes a right-hand operand of type 'ranges::filter_view<ranges::filter_view<ranges::ref_view<std::vector<int,std::allocator<int>>>,Arg>,main::<lambda_2>>' (or there is no acceptable conversion)

So basically how can I apply views::filter on numbers ,assign the result in results , then apply views::filter on results and then again assign the result in results?

std::vector<int> numbers(100);
std::iota(numbers.begin(), numbers.end(), 1);
auto results = numbers | ranges::views::filter([](int n) {return n != 1; });
results = results | ranges::views::filter([i](int n) {return n == 2
        || n % 2 != 0; });

Source: Windows Questions C++

LEAVE A COMMENT