我正在尝试使用std::min_element
并std::max_element
返回双精度矢量中的min和max元素.我的编译器不喜欢我目前正在尝试使用它们,我不理解错误消息.我当然可以编写自己的程序来查找最小值/最大值,但我想了解如何使用这些函数.
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char** argv) {
double cLower, cUpper;
vector<double> C;
// code to insert values in C not shown here
cLower = min_element(C.begin(), C.end());
cUpper = max_element(C.begin(), C.end());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是编译器错误:
../MIXD.cpp:84: error: cannot convert '__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to 'double' in assignment
../MIXD.cpp:85: error: cannot convert '__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to 'double' in assignment
Run Code Online (Sandbox Code Playgroud)
有人请解释我做错了什么吗?
我想,这应该是显而易见的.但是我找不到简单的方法来在OpenCV的Mat中找到所有像素中的最大值.当然,我可以为每个像素类型执行以下操作.但一般的最大功能仍然有用.
double cvMax(cv::Mat& mat)
{
float max=0;
float* pData=(float*)mat.data;
for(int i=0;i<mat.rows;i++)
{
for(int j=0;j<mat.cols;j++)
{
float value = pData[j+i*mat.cols];
if(value>max)
{
max=value;
}
}
}
return max;
}
Run Code Online (Sandbox Code Playgroud)