map_reduce C++ lambda:候选不可行的单一参数
map_reduce C++ lamba: candidate not viable single argument
我写了一个小程序使 map/reduce 在 C++ 中工作。
这里的结构:
struct Point {
int x = 0;
int y = 0;
};
我想选择每个点的 'x' 并将它们乘以一个整数。然后,我想用最后的 lamba(或通过 std::multiples)
减少值
std::vector<Point> p = {{10,20},{20,40}};
std::vector<int> p3 (p.size());
auto totalx = transform_reduce(p.begin(), p.end(),p3.begin(), 0.0, [](Point& p){ return p.x * 23;}, [](int a, int b){return a * b;});
cout << totalx << endl;
我有这个错误:
main.cpp:28: candidate function not viable: requires single argument 'p', but 2 arguments were provided
conversion candidate of type 'int (*)(Point &)'
而且我真的不明白我做错了什么。
我觉得你想要:
- 将所有的点从(x, y)变换为x*23
- 将所有这些值相乘
在这种情况下,您应该给出:
- 开始
- 结束
- 初始值(乘法时为 1.0 而不是 0.0)
- 二元运算(
std::multiplies<int>()
,或你的[](int a, int b) {return a * b; }
,以减少元素之间)
- 映射单个元素的一元运算符(在您的例子中,
[](const int number){return number*23;}
)
总体:
auto totalx = std::transform_reduce(p.begin(), p.end(), 1.0, [](int a, int b) {return a * b; }, [](const Point& p) { return p.x * 23; });
请注意,不需要输出迭代器,因为只有一个结果。
我写了一个小程序使 map/reduce 在 C++ 中工作。 这里的结构:
struct Point {
int x = 0;
int y = 0;
};
我想选择每个点的 'x' 并将它们乘以一个整数。然后,我想用最后的 lamba(或通过 std::multiples)
减少值std::vector<Point> p = {{10,20},{20,40}};
std::vector<int> p3 (p.size());
auto totalx = transform_reduce(p.begin(), p.end(),p3.begin(), 0.0, [](Point& p){ return p.x * 23;}, [](int a, int b){return a * b;});
cout << totalx << endl;
我有这个错误:
main.cpp:28: candidate function not viable: requires single argument 'p', but 2 arguments were provided
conversion candidate of type 'int (*)(Point &)'
而且我真的不明白我做错了什么。
我觉得你想要:
- 将所有的点从(x, y)变换为x*23
- 将所有这些值相乘
在这种情况下,您应该给出:
- 开始
- 结束
- 初始值(乘法时为 1.0 而不是 0.0)
- 二元运算(
std::multiplies<int>()
,或你的[](int a, int b) {return a * b; }
,以减少元素之间) - 映射单个元素的一元运算符(在您的例子中,
[](const int number){return number*23;}
)
总体:
auto totalx = std::transform_reduce(p.begin(), p.end(), 1.0, [](int a, int b) {return a * b; }, [](const Point& p) { return p.x * 23; });
请注意,不需要输出迭代器,因为只有一个结果。