I have a question about finding the maximum and minimum elements in a list. (The list in question will be quite small -- only 2 elements -- so I am hoping for an efficient solution to all of this.) Is there a way to do this simply and efficiently? I am making my switch over from Matlab and Mathematica to C++, so I was expecting to find a pre-defined function for this. I was surprised to learn that this function isn't part of math.h. Thanks!
by list do you mean std::list or an array? If you mean std::list you should not expect to find it in math.h (or cmath) because that is an old C header and C didn't have std::list. You can use std::max_element and std::min_element to get the max and min value of the list.
vector<int> l;
// fill the vector
std::vector<int>::iterator max = std::max_element(l.begin(), l.end());
std::vector<int>::iterator min = std::min_element(l.begin(), l.end());