• c++ STL 最大值最小值


    #include <iostream>
    #include <algorithm>
    #include <deque>

    using namespace std;

    //二元谓词
    //比较两个数的大小
    bool absLess(int elementParam1,int elementParam2)
    {
      return abs(elementParam1) < abs(elementParam2);
    }

    int main()
    {
      deque<int> deque1;
      for (int k=0;k<10;k++)
      {
        deque1.insert(deque1.end(),k);
      }

      for (int k = -4; k<6; k++)
      {
        deque1.insert(deque1.end(), k);
      }

      deque<int>::iterator deque_iter;
      for (deque_iter = deque1.begin();deque_iter != deque1.end();++deque_iter)
      {
        cout << *deque_iter << " ";
      }
      cout << endl;

      //
      deque<int>::iterator min_index = min_element(deque1.begin(), deque1.end());
      cout << *min_index << endl;

      int min_value = *min_element(deque1.begin(), deque1.end());
      cout << min_value << endl;

      deque<int>::iterator max_index = max_element(deque1.begin(),deque1.end());
      cout << "max:" << *max_index << endl;

      bool b1 = absLess(*min_index,*max_index);
      cout << b1 << endl;

      //绝对最小值
      deque<int>::iterator min_index2 = min_element(deque1.begin(),deque1.end(),absLess);
      cout << "绝对最小值:" << abs(*min_index2) << endl;

      //绝对最大值
      deque<int>::iterator max_index2 = max_element(deque1.begin(),deque1.end(), absLess);
      cout << "绝对最大值:" << abs(*max_index2) << endl;

      system("pause");
      return 0;
    }

    ============================

    0 1 2 3 4 5 6 7 8 9 -4 -3 -2 -1 0 1 2 3 4 5
    -4
    -4
    max:9
    1
    绝对最小值:0
    绝对最大值:9
    请按任意键继续. . .

  • 相关阅读:
    Django异步与定时任务Celery
    SkyWalking 分布式追踪系统
    SharePoint 2010 硬件详细需求
    使用SharePoint 2010 客户端对象模型进行文档库及文档的操作
    SharePoint 2010 部署架构
    【git】项目添加.gitignore忽略.idea文件夹
    Android 横屏切换竖屏Activity的生命周期(转)
    经过完整测试的农历公历相互转换
    TZ ERP项目的随想
    C#加密与解密
  • 原文地址:https://www.cnblogs.com/herd/p/11000119.html
Copyright © 2020-2023  润新知