- 開發環境
- 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;