在强大的STL库中存在一个神奇的函数,那就是nth_element,这个函数主要用来将数组元素中第k小的整数排出来并在数组中就位,随时调用,可谓十分实用。
一、nth_element()函数的用法:
函数语句:nth_element(数组名,数组名+第k小元素,数组名+元素个数)
该函数仅排序第n个元素(从0开始索引),即将位置n(从0开始)的元素放在第n大的位置,处理完之后,默认排在它前面的元素都不比它大,排在它后面的元素都不比它小。
二、头文件:
在使用此函数时需要有如下头文件:
#include <algorithm>
三、测试代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 10};
int k = 2; //第k小的数
cout << "原数组:";
for(int i = 0; i < 10; i++) cout << a[i] << " ";
cout << endl;
sort(a, a + 10);
cout << "由小到大排序后的数组:";
for(int i = 0; i < 10; i++) cout << a[i] << " ";
cout << endl;
cout << "第k小的数:";
nth_element(a, a+k, a+10);
cout << a[k] << endl;
return 0;
}