Vector求最大值最小值


QVector <double> data {11.0, 44.0, 22.0, 33.0, 10.0,65.0};//表示最大值:
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 positionmax = std::distance(std::begin(data),max);
auto positionmin = std::distance(std::begin(data),min);
int posmax = positionmax;
int posmin = positionmin;

qDebug()<<"biggest = "<<biggest;
qDebug()<<"smallest = "<<smallest;
qDebug()<<"pos ="<<posmax;
qDebug()<<"posmin = "<<posmin;

 

这里用的QVector,std::vector应该也可以


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM