stl nth_element使用...

#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个位置...

你可能感兴趣的:(stl nth_element使用...)