0
点赞
收藏
分享

微信扫一扫

c++模板库排序求平均

AbrahamW 2022-01-06 阅读 25

排序、得到最大值、最小值、平均值

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>

using namespace std;
int main()
{
    std::vector<float> v{ 0.200000, 0.200000, 0.100000, 0.200000, 0.200000, 0.200000, 0.200000, 0.200000, 0.200000, 0.200000, 0.200000, 0.100000 };
    std::sort(v.begin(), v.end());
    std::cout << "min:" << *min_element(v.begin(), v.end()) << std::endl;
    std::cout << "max:" << *max_element(v.begin(), v.end()) << std::endl;
    std::cout << "sum:" << accumulate(v.begin(), v.end(), 0.000000) << std::endl;
    std::cout << "aver:" << accumulate(v.begin(), v.end(), 0.000000) / v.size() << std::endl;
    return 0;
}

输出

min:0.1
max:0.2
sum:2.2
aver:0.183333

举报

相关推荐

0 条评论