How can I get the maximum or minimum value in a vector?

Using C++11/C++0x compile flags, you can auto it = max_element(std::begin(cloud), std::end(cloud)); // C++11 Otherwise, write your own: template <typename T, size_t N> const T* mybegin(const T (&a)[N]) { return a; } template <typename T, size_t N> const T* myend (const T (&a)[N]) { return a+N; } See it live at http://ideone.com/aDkhW: #include <iostream> #include <algorithm> … Read more

MIN and MAX in C

Where are MIN and MAX defined in C, if at all? They aren’t. What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred). As functions. I wouldn’t use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan … Read more

Getting the index of the returned max or min item using max()/min() on a list

Say that you have a list values = [3,6,1,5], and need the index of the smallest element, i.e. index_min = 2 in this case. Avoid the solution with itemgetter() presented in the other answers, and use instead index_min = min(range(len(values)), key=values.__getitem__) because it doesn’t require to import operator nor to use enumerate, and it is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)