c++ - 与自定义对象一起使用 accumulate

标签 c++ opencv c++11

我在 C++ 中使用 opencv 库,我正在尝试计算 vector<Point2f> difference 中包含的点的总和

Point 类具有 x 属性,即 float .

float pointSumX(Point2f pt1,Point2f pt2)
{
    return (pt1.x + pt2.x);
}

我定义了上面的函数,并从如下所示的 accumulate 中调用它。但它会引发错误。

float avgMotionX = accumulate(difference.begin(),difference.end(),0,pointSumX);

错误是:

error: could not convert ‘__init’ from ‘int’ to ‘cv::Point_’ __init = __binary_op(__init, *__first);

注意:我使用的是 C++11

最佳答案

float pointSumX(Point2f pt1, Point2f pt2)

应该是

float pointSumX(float lhs, const Point2f& rhs)
{
    return lhs + rhs.x;
}

因为 lhs 是累加器。

另请注意,您应该调用它

std::accumulate(difference.begin(), difference.end(), 0.f, pointSumX); // 0.f instead of 0

返回 float 而不是 int

关于c++ - 与自定义对象一起使用 accumulate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39860788/

相关文章:

python - Python获取多个Button点击事件的方法

c++ - 从命令提示符执行 C++ 程序

c++ - 如何在C++中比较std::array?

c++ - vector 迭代器不递增

c++ - GpuMat - 访问自定义内核中的 2 channel float 据

c++ - opencv2/tracking.hpp : No such file or directory on Ubuntu 16. 04

c++ - 有没有办法(在 C++ 中)创建一个实现某些功能的模板类?

c++ - Boost.Spirit 语法。属性和 _val 问题

c++ - 严格违反别名规则

c++ - opencv中的最小值和最大值