zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    python day 09 文件操作
    day python calss08 深浅copy
    python.day05
    pytonn04day
    python开发day03
    python开发day02
    python 变量名的规范
    设备描述表
    GDI基础
    windows编程-窗口
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14313486.html
Copyright © 2011-2022 走看看