Qt 獲取容器Vector中的最大值和最小值


獲取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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM