How to create a cartesian product range from filtered data?

匆匆过客 提交于 2019-12-11 03:36:19

问题


I am trying to create a cartesian-product range out of smaller ranges. I thought ranges::v3::view::cartesian_product would work, but somehow it doesn't.

If I try to create a cartesian product using containers directly, I have no problem. The following compiles:

#include <vector>
#include <range/v3/view/cartesian_product.hpp>

int main() {
    std::vector<int> data1{1,5,2,7,6,3,4,8,9,0};
    std::vector<int> data2{1,5,2,7,6,3,4,8,9,0};
    auto range = ranges::v3::view::cartesian_product(data1, data2);
}

However, as soon as I start using filters:

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

int main() {
    std::vector<int> data1{1,5,2,7,6,3,4,8,9,0};
    std::vector<int> data2{1,5,2,7,6,3,4,8,9,0};
    auto range = ranges::v3::view::cartesian_product(
            data1 | ranges::v3::view::filter([](int v) { return v%2; }),
            data2);
}

I get tons of hard-to-decipher compile errors, starting with:

In file included from contrib/range/v3/view/cartesian_product.hpp:21:0,
                 from cartesian-err.cpp:2:
contrib/range/v3/range_concepts.hpp: In instantiation of ‘class ranges::v3::cartesian_product_view<ranges::v3::remove_if_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > >, ranges::v3::logical_negate_<main()::<lambda(int)> > >, ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > > >’:
cartesian-err.cpp:10:18:   required from here
contrib/range/v3/range_concepts.hpp:78:50: error: no match for call to ‘(const ranges::v3::_begin_::fn) (const ranges::v3::remove_if_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> > >, ranges::v3::logical_negate_<main()::<lambda(int)> > >&)’
                 using iterator_t = decltype(begin(std::declval<T &>()));
                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~

How do I get around it?

P.S. Is there somewhere some good documentation of the ranges-v3 library? I can't find any and I feel I am walking in the dark...


回答1:


Bug or not, one can get around the cartesian_product by implementing it manually, as suggested in https://github.com/ericniebler/range-v3/issues/173.

The added benefit is, that you have better control on the order of iteration, which may have a performance impact if the filter function is expensive.

In the above case, one can implement it like this (shortened the input vectors for brevity):

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

int main() {
    std::vector<int> data1{1,5,2,7,6};
    std::vector<int> data2{1,5,2,7,6};
    auto range =
            data1
            | ranges::v3::view::filter([](int v) { return v%2; })
            | ranges::v3::view::for_each([&](int v) {
                return data2 | ranges::v3::view::for_each([v](int v2) {
                    return ranges::v3::yield(std::make_pair(v,v2));
                });
            });
    for (auto&& pair : range) {
        std::cout << "[" << pair.first << "," << pair.second << "]\n";
    }
    return 0;
}

gives the expected output:

[1,1]
[1,5]
[1,2]
[1,7]
[1,6]
[5,1]
[5,5]
[5,2]
[5,7]
[5,6]
[7,1]
[7,5]
[7,2]
[7,7]
[7,6]


来源:https://stackoverflow.com/questions/54029428/how-to-create-a-cartesian-product-range-from-filtered-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!