您的位置:首页 > 其它

stl nth_element使用...

2010-07-29 15:01 281 查看
#include <algorithm>
#include <vector>
#include <ctime>
#include <iostream>
using namespace std;
const int MAX = 50;
int main(int argc, char *argv[])
{
vector<int> vec;
int each;
int count = 10;

srand((unsigned)time(NULL));

for(int i=0; i<count; i++)
{
each = rand() % MAX;
vec.push_back(each);
}

cout << "before nth_element: " << endl;
for(int i=0; i<count; i++)
cout << vec.at(i) << " ";
cout << endl;

nth_element(vec.begin(), vec.begin() + 5, vec.end());

cout << "after nth_element: " << endl;
for(int i=0; i<count; i++)
cout << vec.at(i) << " ";
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
 

将第N小的元素放在第N个位置...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  each include system null