将第n大元素放在序列第n个位置,且该位置的左边元素都小于该元素,该位置的右边元素都不小于该元素。
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v{ 9,2,7,3,1,6,8,4,5,2,4 };
nth_element(v.begin(), v.begin() + 4, v.end());
for (auto item : v)
cout << item << "->";
cout << endl;
cout << "5th:" << *(v.begin() + 4) << endl;
return 0;
}
输出结果:
1->2->2->3->4->4->5->6->7->8->9->
5th:4