Home:ALL Converter>Find minimum and maximum of a long vector

Find minimum and maximum of a long vector

Ask Time:2017-07-27T20:23:02         Author:schorsch312

Json Formatter

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


}

Author:schorsch312,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45350731/find-minimum-and-maximum-of-a-long-vector
yy