已知一个vector<double>,求其最大值最小值和平均数:
#include <numeric>
#include <algorithm>
void calculateStats(const vector<double> &data, double &max, double &min, double &average){
if(data.size()==0){
max =0;
min = 0;
average =0;
return;
}
double sum = accumulate(begin(data), end(data), 0.0);
double mean = sum / data.size();
max = *max_element(data.begin(), data.end());
min = *min_element(data.begin(), data.end());
average = mean;
}