C++中nth_element函数的用法
来源:互联网 发布:投资理财系统源码 编辑:程序博客网 时间:2024/05/28 05:13
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 andlast, including the element pointed by first but not the element pointed bylast.
Notice that in this function, these are not consecutive parameters, but the first andthird 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, returnstrue if the first argument goes before the second argument in the specificstrict 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函数的方式进行比较
- C++中nth_element函数的用法
- C++中nth_element函数的用法
- C/C++中nth_element函数
- nth_element的用法
- nth_element的用法
- nth_element 用法简介
- C语言中rand()函数的用法
- C语言中getenv函数的用法
- C语言中函数指针的用法
- c语言中time函数的用法
- C语言中sprintf()函数的用法
- C语言中getenv函数的用法
- c 中typedef函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C语言中umask函数的用法
- C语言中getenv函数的用法
- c中 typedef函数的用法
- PHP获取当前登陆用户IP
- Spring3无法显示css/js/jpg/gif
- 行人检测HOG+SVM
- 设计中的五行
- UIView你知道多少
- C++中nth_element函数的用法
- WDK/WinDDK setenv 路径设置
- 技术人员的职业规划
- PHP中$_SERVER的浅析
- PE文件加载和修改
- iOS 和 Android 的后台推送工作原理各是如何?有什么区别?
- 关于line-height,font-size等
- 一致性 hash 算法( consistent hashing )
- Android数据存储---SharedPreferences