STL中的nth_element()方法的使用
2008-06-22 00:09
417 查看
STL中的nth_element()方法的使用 通过调用nth_element(start, start+n, end) 方法可以使第n大元素处于第n位置(从0开始,其位置是下标为 n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的,下面是这个方法的具体使用方法.
#pragma warning(disable: 4786) #include <iostream> #include <algorithm> #include <functional> #include <vector> using namespace std; int main() { const int VECTOR_SIZE = 50 ; vector<int> Numbers(VECTOR_SIZE) ; vector<int>::iterator start, end, it ; // Initialize vector Numbers for(int i=0;i<50;++i){ Numbers[i]=i; } /*由于赋值时是有序的,下面random_shuffle()方法将这些数据的顺序打乱*/ random_shuffle(Numbers.begin(),Numbers.end()); // location of first element of Numbers start = Numbers.begin() ; // one past the location last element of Numbers end = Numbers.end() ; cout << "Before calling nth_element/n" << endl ; // print content of Numbers cout << "Numbers { " ; for(it = start; it != end; it++) cout << *it << " " ; cout << " }/n" << endl ; /* * partition the elements by the 8th element, *(notice that 0th is the first element) */ nth_element(start, start+8, end) ; cout << "After calling nth_element/n" << endl ; cout << "Numbers { " ; for(it = start; it != end; it++) cout << *it << " " ; cout << " }/n" << endl ; system("pause"); }
相关文章推荐
- 浅谈 STL中的 nth_element() 使用方法
- STL<algorithm>中的nth_element()方法的使用
- STL中的nth_element()方法的使用
- STL中的nth_element()方法的使用
- STL中的nth_element()方法的使用
- STL 源码分析《2》----nth_element() 使用与源码分析
- stl nth_element使用...
- STL 源码分析《2》----nth_element() 使用与源码分析
- dom解析xml中的getElementById方法的使用
- STL之set使用方法小结
- cSS3 伪类:nth-child 的使用方法
- POJ1007 关于STL排序方法 动态数组的创建 和向量的使用
- nth_element()方法总结
- STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL 中优先队列的使用方法(priority_queue)
- mfc中使用标准库stl出现xdebug、xlocale错误的解决方法
- STL之Deque的使用方法
- Selenium中expected_conditions下text_to_be_present_in_element_value方法的使用
- C++ 标准模板库STL set 使用方法与应用介绍(一)
- document.getElementById()方法使用