gpt4 book ai didi

c++ - 如何从过滤后的数据创建笛卡尔积范围?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:36 25 4
gpt4 key购买 nike

我正在尝试从更小的范围创建笛卡尔积范围。我认为 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 &>()));
~~~~~^~~~~~~~~~~~~~~~~~~~~

我该如何绕过它?

附言有没有关于 ranges-v3 库的一些好的文档?我找不到任何东西,我觉得我在黑暗中行走......

最佳答案

无论是否有错误,都可以按照 https://github.com/ericniebler/range-v3/issues/173 中的建议通过手动实现来绕过 cartesian_product .

额外的好处是,您可以更好地控制迭代顺序,如果过滤器函数开销很大,这可能会对性能产生影响。

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

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

关于c++ - 如何从过滤后的数据创建笛卡尔积范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54029428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com