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 轉爲數組

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