如何根据过滤后的数据创建笛卡尔乘积范围? [英] How to create a cartesian product range from filtered data?

查看:116
本文介绍了如何根据过滤后的数据创建笛卡尔乘积范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我该如何解决?

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

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...

推荐答案

是否存在问题,如

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

给出预期的输出:

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

这篇关于如何根据过滤后的数据创建笛卡尔乘积范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆