获取qt容器中的最大值和最小值的做法:
一、加上头文件
#include <QVector>
二、加上如下的代码:
1 QVector <double> data {11.0, 44.0, 22.0, 33.0, 10.0,65.0}; 2 //第一种表示最大值: 3 // QVector<double>::iterator max = std::max_element(std::begin(data), std::end(data)); 4 //第二种表示最大值:
5 auto max = std::max_element(std::begin(data), std::end(data)); 6 //最小值表示:
7 auto min = std::min_element(std::begin(data), std::end(data)); 8 //直接赋值表示
9 double biggest = *max; 10 double smallest = *min; 11 //最大值和最小值的位置的表示方式:
12 auto positionmax = std::distance(std::begin(data),max); 13 auto positionmin = std::distance(std::begin(data),min); 14 int posmax = positionmax; 15 int posmin = positionmin; 16
17 qDebug()<<"biggest = "<<biggest; 18 qDebug()<<"smallest = "<<smallest; 19 qDebug()<<"pos ="<<posmax; 20 qDebug()<<"posmin = "<<posmin;
使用第二种方式找到最大值:
1 for(auto y2:data) 2 { 3 if(qAbs(y2)>ymax) 4 { 5 ymax = qAbs(y2); 6 } 7 }