zoukankan      html  css  js  c++  java
  • 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
    请按任意键继续. . .

  • 相关阅读:
    oracle 排序字段自增长
    ORACLE REPLACE函数
    oracle非空不做更新
    Elasticsearch 5.4.3 聚合分组
    Elasticsearch 版本控制
    Elasticsearch 配置同义词
    Elasticseach的评分机制
    实现Ecshop商品跳到淘宝、京东等的购买链接
    修改ECSHOP系统红包序列号规律
    Ecshop后台订单列表增加”商品名”检索字段
  • 原文地址:https://www.cnblogs.com/herd/p/11000119.html
Copyright © 2011-2022 走看看