如何根据过滤后的数据创建笛卡尔乘积范围?

时间:2019-01-03 20:35:07

标签: c++ range-v3

我正在尝试创建较小范围内的笛卡尔乘积范围。我以为ranges::v3::view::cartesian_product可以工作,但不知何故。

如果我尝试直接使用容器创建笛卡尔积,则没有问题。编译如下:

#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);
}

但是,一旦我开始使用过滤器:

#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);
}

从以下内容开始,我收到大量难以理解的编译错误:

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 &>()));
                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~

我该如何解决?

P.S。是否有一些有关range-v3库的好文档?我什么也找不到,我觉得我在黑暗中行走...

1 个答案:

答案 0 :(得分:0)

无论是否出现问题,都可以按照https://github.com/ericniebler/range-v3/issues/173中的建议通过手动实施cartesian_product来解决。

额外的好处是,您可以更好地控制迭代的顺序,如果过滤器功能昂贵,则可能会对性能产生影响。

在上述情况下,可以这样实现(为简洁起见,缩短了输入向量):

#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;
}

给出预期的输出:

[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]