std::vector<int> vec; ... int max_value = 0; for ( int i=0 ; i<(int)vec.size() ; i++ ) { if (vec[i] > max_value) { max_value = vec[i]; } }
I was wondering why some developer always try to reinvent the wheel ?
C++ comes with a tons of useful algorithm to do that kind of stuff, and with an explicit name. In example that code can be rewrite with:
#include <algorithm> ... std::vector<int> vec; ... int max_value = *std::max_element<std::vector<int>::iterator >(vec.begin(), vec.end());
it's shorter and more explicit...
Note for C++11 users:
auto max_value = *std::max_element<std::vector<int>::iterator >(begin(vec), end(vec));