c++ - 使用带有 max_element 的 lambda 函数编译错误

标签 c++ algorithm iterator declaration

我正在尝试编写一些代码以在轮廓的 std::vector 中找到具有最大尺寸的轮廓。

我有以下错误

error: conversion from ‘__gnu_cxx::__normal_iterator<std::vector<cv::Point_<int> >*, std::vector<std::vector<cv::Point_<int> > > >’ to non-scalar type
‘std::vector<cv::Point_<int> >::iterator {aka __gnu_cxx::__normal_iterator<cv::Point_<int>*, std::vector<cv::Point_<int> > >}’ requested
    std::vector<cv::Point2i>::iterator it = std::max_element(contours.begin(), contours.end()

下面是我的代码

std::vector<std::vector <cv::Point2i>> contours;
std::vector<cv::Vec4i> hierarchy;

cv::findContours(rImg, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cv::Point(0, 0));
cv::Mat blank = cv::Mat::zeros(frame.size(), CV_8UC3);
cv::RNG rng;

std::vector<cv::Point2i>::iterator it = std::max_element(contours.begin(), contours.end(),
                                                        [](const std::vector<cv::Point2i>& p1, 
                                                        const std::vector<cv::Point2i>& p2)
                                                        { return p1.size()< p2.size(); });


std::vector<std::vector<cv::Point2i> > contourV;
contourV.push_back(it);

想知道哪里出了问题以及如何改正

最佳答案

您正在使用该类型的对象

std::vector<std::vector <cv::Point2i>> contours;

std::max_element 算法中

因此迭代器应对应于该容器。也就是

std::vector<std::vector <cv::Point2i>>::iterator it = std::max_element( /*...*/ );

或者写起来更简单

auto it = std::max_element(contours.begin(), contours.end(),
                           [](const std::vector<cv::Point2i>& p1, 
                              const std::vector<cv::Point2i>& p2)
                              { return p1.size()< p2.size(); });

正如 @melpomene 在评论中指出的那样。

关于c++ - 使用带有 max_element 的 lambda 函数编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56045041/

相关文章:

c# - 传递结构似乎会损坏数据

c++ - 使用对象参数还是使用成员对象更好?

c++ - 基本 C++ 套接字客户端

c++ - 将使用 QML、QBS 构建的 Qt 应用程序部署到 Windows

c++ - 从成员函数返回 boost iterator_range

python - Python 读取文件时跳过几行

algorithm - 遗传算法中的错误( Octave )

algorithm - 伪代码:递归处理列表中的开始/停止时间

algorithm - Map-Reduce实现中的一种特殊示例方法

C++模板类映射