共用方式為


nth_element

分割,正確設定這個序列中的 第 n個元素的元素範圍範圍,讓前面的所有項目小於或等於它,並遵循此序列中的所有項目大於或等於它。

template<class RandomAccessIterator>
   void nth_element(
      RandomAccessIterator _First, 
      RandomAccessIterator _Nth, 
      RandomAccessIterator _Last
   );
template<class RandomAccessIterator, class BinaryPredicate>
   void nth_element(
      RandomAccessIterator _First, 
      RandomAccessIterator _Nth, 
      RandomAccessIterator _Last,
      BinaryPredicate _Comp
   );

參數

  • _First
    解決的隨機存取Iterator第一個項目的位置會分割的範圍。

  • _Nth
    解決的隨機存取Iterator在分割的界限正確地排程之項目的位置。

  • _Last
    解決的隨機存取Iterator超過最後一個項目的位置是在要分割的範圍。

  • _Comp
    定義連續項目會滿足比較準則順序的使用者定義之述詞函式物件。 一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。

備註

參考的範圍必須是有效的,任何指標必須 dereferenceable,並在序列中最後一個位置開始可取得的會增加。

nth_element 演算法並不保證會在子範圍的項目 第n個項目的任一邊排序。 它會 partial_sort以執行較少,確保在某一個選取的項目下以這個範圍的項目,可以做為多電腦環境中的 partial_sort 的更快速的替代方案,而不需要期間排定較小範圍的。

如果兩者都比其他,不小於項目相等,則為,但不一定會等於。

平均相同類別的複雜為線性。如需 _Last – _First

範例

// alg_nth_elem.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>      // For greater<int>( )
#include <iostream>

// Return whether first element is greater than the second
bool UDgreater ( int elem1, int elem2 ) {
   return elem1 > elem2;
}

int main() {
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter1;

   int i;
   for ( i = 0 ; i <= 5 ; i++ )
      v1.push_back( 3 * i );

   int ii;
   for ( ii = 0 ; ii <= 5 ; ii++ )
      v1.push_back( 3 * ii + 1 );

   int iii;
   for ( iii = 0 ; iii <= 5 ; iii++ )
      v1.push_back( 3 * iii +2 );

   cout << "Original vector:\n v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   nth_element(v1.begin( ), v1.begin( ) + 3, v1.end( ) );
   cout << "Position 3 partitioned vector:\n v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   // To sort in descending order, specify binary predicate
   nth_element( v1.begin( ), v1.begin( ) + 4, v1.end( ),
          greater<int>( ) );
   cout << "Position 4 partitioned (greater) vector:\n v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;
   
   random_shuffle( v1.begin( ), v1.end( ) );
   cout << "Shuffled vector:\n v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   // A user-defined (UD) binary predicate can also be used
   nth_element( v1.begin( ), v1.begin( ) + 5, v1.end( ), UDgreater );
   cout << "Position 5 partitioned (UDgreater) vector:\n v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;
}

範例輸出

Original vector:
 v1 = ( 0 3 6 9 12 15 1 4 7 10 13 16 2 5 8 11 14 17 )
Position 3 partitioned vector:
 v1 = ( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 )
Position 4 partitioned (greater) vector:
 v1 = ( 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 )
Shuffled vector:
 v1 = ( 5 16 8 15 17 6 10 0 13 2 9 12 3 4 7 1 11 14 )
Position 5 partitioned (UDgreater) vector:
 v1 = ( 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 )

需求

標題: <algorithm>

命名空間: std

請參閱

參考

nth_element (STL Samples)

Predicate Version of nth_element

標準樣板程式庫