在向量< double>上使用std :: max_element [英] Using std::max_element on a vector<double>
问题描述
我试图使用 std :: min_element
和 std :: max_element
返回最小和最大在向量中的元素。我的编译器不喜欢我目前正试图使用它们,我不明白错误消息。我当然可以写我自己的程序来找到最小/最大值,但我想了解如何使用这些函数。
I'm trying to use std::min_element
and std::max_element
to return the min and max elements in a vector of doubles. My compiler doesn't like how I'm currently trying to use them, and I don't understand the error message. I could of course write my own procedure to find the min/max, but I'd like to understand how to use the functions.
#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;
}
这里是编译器错误:
../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
有人请解释我做错了什么? / p>
Would someone please explain what I'm doing wrong?
推荐答案
min_element
和 max_element
返回迭代器,而不是值。所以你需要 * min_element ...
和 * max_element ...
。
min_element
and max_element
return iterators, not values. So you need *min_element...
and *max_element...
.
这篇关于在向量< double>上使用std :: max_element的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!