3
我想查找長矢量的最小值和最大值。下面的代碼有效,但我需要遍歷向量兩次。查找長矢量的最小值和最大值
我可以使用老式的循環,但我不知道是否有一個優雅的(c + + 11,標準)的方式做到這一點。
#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
}
[一個很好的參考](http://en.cppreference.com/w/cpp/algorithm)總是很方便。 –