标题其实就是nth_element函数的底层实现
nth_element(first, nth, last, compare)
求[first, last]这个区间中第n大小的元素
如果参数加入了compare函数,就按compare函数的方式比较
array[first, last)元素区间,排序后,array[nth]就是第n大的元素(从0开始)
#include<iostream>
#include<algorithm>
using namespace std; int main(){
int array[] = {,,,,,,,,};
int len=sizeof(array)/sizeof(int);
cout<<"排序前: ";
for(int i=; i<len; i++)
cout<<array[i]<<" ";
nth_element(array, array+, array+len); //排序第6个元素
cout<<endl;
cout<<"排序后:";
for(int i=; i<len; i++)
cout<<array[i]<<" "; cout<<endl<<"第6个元素为"<<array[]<<endl;
}