c++ Whats the proper way of finding a max value in a vector?

This is part of my main code that is giving me compiler issues.

//Find max value
max = max_element(vecTemperature.begin(), vecTemperature.end());

I keep getting an error saying "[Error] cannot convert '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' to 'int' in assignment"
1
2
3
4
5
if( !vecTemperature.empty() )
{
    const int max = *max_element(vecTemperature.begin(), vecTemperature.end());
    // use max
}
Last edited on
In addition to one JLBorges said, you could sort it by greatest value and assign the first element to a variable.

1
2
sort(vectorName.begin(), vectorName.end(), greater<int>());
    largest = vectorName[0];
Topic archived. No new replies allowed.