std::nth_element
template <class RandomAccessIterator> void nth_element ( RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void nth_element ( RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp );
Rearranges the elements in the range [first,last), in such a way that the element at the resultingnth position is the element that would be in that position in a sorted sequence, with none of the elements preceding it being greater and none of the elements following it smaller than it. Neither the elements preceding it nor the elements following it are guaranteed to be ordered.
The elements are compared using operator< for the first version, and comp for the second.
Parameters
-
first, last
-
Random-Access iterators to the initial and final positions of the sequence to be used. The range used is
[first,last), which contains all the elements between
first and
last, including the element pointed by
first but not the element pointed by
last.
Notice that in this function, these are not consecutive parameters, but the first and third ones.
nth
- Random-Access iterator pointing to the location within the range [first,last) that will have the sorted element. comp
- Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
// 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( ) );
//
//nth_element(v1.begin( ), v1.begin( ) + 18, v1.end( ) );
//cout << "Position 18 partitioned vector:\n v1 = ( " ;
cout << "Position 3 partitioned vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
cout<<v1[3]<<endl; //输出第3大的元素
// To sort in descending order, specify binary predicate
nth_element( v1.begin( ), v1.begin( ) + 4, v1.end( ),
greater<int>( ) ); //greater<int>由大到小排序
cout << "Position 4 partitioned (greater) vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
cout<<v1[4]<<endl; //输出第4大的元素(从0开始排的)
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;
cout<<v1[5]<<endl; //输出第5大的元素,第0大,第1大....
}
测试结果:
nth_element只是将位置为n(从0开始)的元素放到第n大的位置,处理完之后,默认排在它前面的元素都不比它大,排在它后面的元素都不比它小,并且有v[n]分开的两段,各段内部,没有进行明显的排序,但是在vs里面,却将所有的顺序都给排好了。加入comp函数之后,按comp函数的方式进行比较