Find minimum and maximum of a long vector

Viewed 116

I want to find both the minimum and maximum of a long vector. The following code works, but I need to traverse the vector twice.

I could use an old fashioned for loop, but I wonder if there is an elegant (c++11, std) way of doing it.

#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char** argv) {
     vector<double> C;

     // code to insert values in C not shown here

     const double cLower = *min_element(C.begin(), C.end());
     const double cUpper = *max_element(C.begin(), C.end());

     // code using cLower and cUpper


}
1 Answers
Related