C++ 求vector的最大值最小值和平均数

已知一个vector,求其最大值最小值和平均数:

#include 
#include 

void calculateStats(const vector &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;
}

 

你可能感兴趣的:(Algorithm)