Using std::max_element on a vector<double>
c++, max, min, vector
Solution
`min_element` and `max_element` return iterators, not values. So you need `*min_element...` and `*max_element...`.
Problem
I'm trying to use `std::min_element` and `std::max_element` to return the minimum and maximum 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 minimum and maximum, 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 is not shown here cLower = min_element(C.begin(), C.end()); cUpper = max_element(C.begin(), C.end()); return 0; } ``` Here is the compiler error: ``` ../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 ``` What am I doing wrong?