【发布时间】:2012-04-26 21:01:03
【问题描述】:
我正在尝试使用 std::min_element 和 std::max_element 返回双精度向量中的最小和最大元素。我的编译器不喜欢我目前尝试使用它们的方式,而且我不理解错误消息。我当然可以编写自己的程序来找到最小值和最大值,但我想了解如何使用这些函数。
#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 is 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
我做错了什么?
【问题讨论】:
-
请注意,如果您使用的是 C++11,则可以使用
std::minmax_element。 -
@GManNickG:我不知道那个函数。由于行为不同,我在答案中添加了一条注释以将其合并。