STL中的nth_element()方法的使用

STL中的nth_element()方法的使用 通过调用nth_element(start, start+n, end) 方法可以使第n大元素处于第n位置(从0开始,其位置是下标为 n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的,下面是这个方法的具体使用方法.

  1. #pragma warning(disable: 4786)  
  2.  
  3. #include <iostream>  
  4.  
  5. #include <algorithm>  
  6.  
  7. #include <functional>  
  8.  
  9. #include <vector>  
  10.   
  11. using namespace std;  
  12.   
  13.   
  14.   
  15. int main()  
  16.   
  17. {  
  18.   
  19.     const int VECTOR_SIZE = 50 ;  
  20.   
  21.   
  22.   
  23.     vector<int> Numbers(VECTOR_SIZE) ;  
  24.   
  25.   
  26.   
  27.     vector<int>::iterator start, end, it ;  
  28.   
  29.   
  30.   
  31.     // Initialize vector Numbers  
  32.   
  33.     for(int i=0;i<50;++i){  
  34.   
  35.              Numbers[i]=i;  
  36.   
  37.     }  
  38.   
  39. /*由于赋值时是有序的,下面random_shuffle()方法将这些数据的顺序打乱*/  
  40.   
  41.     random_shuffle(Numbers.begin(),Numbers.end());  
  42.   
  43.       
  44.   
  45. // location of first element of Numbers  
  46.   
  47.     start = Numbers.begin() ;   
  48.   
  49.   
  50.   
  51.  // one past the location last element of Numbers  
  52.   
  53.     end = Numbers.end() ;       
  54.   
  55.   
  56.   
  57.     cout << "Before calling nth_element/n" << endl ;  
  58.   
  59.   
  60.   
  61.   // print content of Numbers  
  62.   
  63.     cout << "Numbers { " ;  
  64.   
  65.     for(it = start; it != end; it++)  
  66.   
  67.         cout << *it << " " ;  
  68.   
  69.     cout << " }/n" << endl ;  
  70.   
  71.   
  72.   
  73.   /*  
  74.  
  75.     * partition the elements by the 8th element, 
  76.  
  77.   *(notice that 0th is the first element) 
  78.  
  79.   */   
  80.   
  81.     nth_element(start, start+8, end) ;  
  82.   
  83.   
  84.   
  85.     cout << "After calling nth_element/n" << endl ;  
  86.   
  87.   
  88.   
  89.     cout << "Numbers { " ;  
  90.   
  91.     for(it = start; it != end; it++)  
  92.   
  93.         cout << *it << " " ;  
  94.   
  95.     cout << " }/n" << endl ;  
  96.   
  97.     system("pause");  
  98.   
  99. }  
#pragma warning(disable: 4786)

#include <iostream>

#include <algorithm>

#include <functional>

#include <vector>

using namespace std;



int main()

{

    const int VECTOR_SIZE = 50 ;



    vector<int> Numbers(VECTOR_SIZE) ;



    vector<int>::iterator start, end, it ;



    // Initialize vector Numbers

    for(int i=0;i<50;++i){

             Numbers[i]=i;

    }

/*由于赋值时是有序的,下面random_shuffle()方法将这些数据的顺序打乱*/

    random_shuffle(Numbers.begin(),Numbers.end());

    

// location of first element of Numbers

    start = Numbers.begin() ; 



 // one past the location last element of Numbers

    end = Numbers.end() ;     



    cout << "Before calling nth_element/n" << endl ;



  // print content of Numbers

    cout << "Numbers { " ;

    for(it = start; it != end; it++)

        cout << *it << " " ;

    cout << " }/n" << endl ;



  /* 

    * partition the elements by the 8th element,

  *(notice that 0th is the first element)

  */ 

    nth_element(start, start+8, end) ;



    cout << "After calling nth_element/n" << endl ;



    cout << "Numbers { " ;

    for(it = start; it != end; it++)

        cout << *it << " " ;

    cout << " }/n" << endl ;

    system("pause");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值