Qt——QVector容器使用,获取最大值、最小值、总和

开发环境
Windows7操作系统
Qt 5.8C++GUI框架
MinGW 5.3.0 32bit编译器
Qt Creator 4.2.1编辑器
#include <QVector>
#include <numeric>
//可以不用包含<QVector> <numeric>

//第一种表示最大值:
// QVector<double>::iterator max = std::max_element(std::begin(data), std::end(data));
//第二种表示最大值:
auto max = std::max_element(std::begin(data), std::end(data));
//最小值表示:
auto min = std::min_element(std::begin(data), std::end(data));

//直接赋值表示
double biggest = *max;
double smallest = *min;

//    auto sum = std::accumulate(std::begin(data), std::end(data),0);
//    int summation = *sum;
/**
* 上两行注释,因为报错:
* error: invalid type argument of unary '*' (have 'int')
int summation = *sum;
                  ^
 解决方法:
使用下面的这行代码
**/
//得总和值并赋值表示:
double summation = std::accumulate(std::begin(data), std::end(data),0);

//    qDebug() << "biggest = " << biggest << ",smallest = " << smallest << ",summation = " << summation;

double result = (biggest - smallest)/summation*2;

参考:
qt中获取容器Vector中的最大值和最小值
QT中QVector的基本用法
qvector 转为数组

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章