• C++ STL 排序查找最大的5个


    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <functional>
    #include <iterator>

    using namespace std;


    int main()
    {
      deque<int> deq1;
      deque<int>::iterator deq_iter1;

      for (int k=0;k<16;k++)
      {
        deq1.push_back(rand());
      }

      for (deq_iter1 = deq1.begin();deq_iter1 != deq1.end();++deq_iter1)
      {
        cout << *deq_iter1 << " ";
      }
      cout << endl;
      cout << "------------------------------"<<endl;

      nth_element(deq1.begin(), deq1.begin() + 5, deq1.end());
      for (deq_iter1 = deq1.begin(); deq_iter1 != deq1.end(); ++deq_iter1)
      {
        cout << *deq_iter1 << " ";
      }
      cout << endl;
      cout << "------------------------------" << endl;

      
      copy(deq1.begin(), deq1.begin() + 5,ostream_iterator<int>(cout," "));
      cout << endl;
      cout << "------------------------------" << endl;


      nth_element(deq1.begin(), deq1.end() - 5, deq1.end());
      copy(deq1.end() - 5,deq1.end(), ostream_iterator<int>(cout, " "));
      cout << endl;
      cout << "------------------------------" << endl;

      system("pause");
      return 0;
    }

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

    41 18467 6334 26500 19169 15724 11478 29358 26962 24464 5705 28145 23281 16827 9961 491
    ------------------------------
    41 491 5705 6334 9961 11478 15724 16827 18467 19169 23281 24464 26500 26962 28145 29358
    ------------------------------
    41 491 5705 6334 9961
    ------------------------------
    24464 26500 26962 28145 29358
    ------------------------------
    请按任意键继续. . .

  • 相关阅读:
    成佛、远不止渡沧海
    导航栏中各按钮在点击当前按钮变色其他按钮恢复为原有色的实现方法(vue、jq、原生js)
    vue动态绑定src加字符串拼接
    对象中那些不注意的用法
    vue实现实时监听文本框内容的变化(最后一种为原生js)
    table
    toFixed()精度丢失;复选框全选、取消
    vue.js
    vue项目知识点总结
    JVM基础知识总结
  • 原文地址:https://www.cnblogs.com/herd/p/11012837.html
Copyright © 2020-2023  润新知