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

  • 相关阅读:
    ASP.NET 取得 Uri 各项属性值
    js获取当前时间显示在页面上
    脚步提示及跳转
    整体刷新和局部刷新frameset窗口
    asp.net 字符串过滤
    .net 获取当前网页的的url
    优酷去广告最新的关于如何屏蔽优酷广告的方法
    bat命令集合
    修复IE
    网易见外工作台(AI),语音转文字,快速制作字幕,中英翻译,在线修改字幕
  • 原文地址:https://www.cnblogs.com/herd/p/11012837.html
Copyright © 2011-2022 走看看