STL中的nth_element()方法的使用
2012-04-09 16:40
405 查看
STL中的nth_element()方法的使用 通过调用nth_element(start, start+n, end) 方法可以使第n大元素处于第n位置(从0开始,其位置是下标为 n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的,下面是这个方法的具体使用方法.
转载地址:http://blog.csdn.net/guofengzai/article/details/2574225#
一下是自己的总结,方便自己理解:
STL中的函数random_shuffle()用来对一个元素序列进行重新排序(随机的),函数原型如下:
#pragma warning(disable: 4786) #include <iostream> #include <algorithm> #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()); start = Numbers.begin() ; end = Numbers.end() ; cout << "Before calling nth_element/n" << endl ; cout << "Numbers { " ; for(it = start; it != end; it++) cout << *it << " " ; cout << " }/n" << endl ; //排序 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"); }
转载地址:http://blog.csdn.net/guofengzai/article/details/2574225#
一下是自己的总结,方便自己理解:
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<int> a; for(int i = 0; i < 50; i++) { a.push_back(i); } std::random_shuffle(a.begin(),a.end());//函数说明见下 for(int j = 0; j < 50; j++) { cout<<a[j]<<" "; } cout<<endl; std::nth_element(a.begin(),a.begin()+8,a.end()); for(int k = 0; k < 50; k++) { cout<<a[k]<<" "; } cout<<endl; system("pause"); return 0; }
STL中的函数random_shuffle()用来对一个元素序列进行重新排序(随机的),函数原型如下:
template<class RandomAccessIterator> void random_shuffle( RandomAccessIterator _First, //指向序列首元素的迭代器 RandomAccessIterator _Last //指向序列最后一个元素的下一个位置的迭代器 );
相关文章推荐
- STL<algorithm>中的nth_element()方法的使用
- STL中的nth_element()方法的使用
- STL中的nth_element()方法的使用
- STL中的nth_element()方法的使用
- 浅谈 STL中的 nth_element() 使用方法
- STL 源码分析《2》----nth_element() 使用与源码分析
- stl nth_element使用...
- STL 源码分析《2》----nth_element() 使用与源码分析
- STL之sort函数的使用方法
- STL:string常用函数方法使用介绍
- STL之set使用方法小结
- C++ STL--stack/queue 的使用方法
- STL 中栈的使用方法(stack)
- Element UI 框架中Loading 区域加载的使用方法
- STL之map的使用方法
- C++ STL中 std::transfrom()..使用方法.【2013.10.25】
- C++ STL--stack/queue 的使用方法
- C++使用STL:慎重选择删除元素的方法
- C++ STL--stack/queue 的使用方法
- Angular.element和$document的使用方法分析,代替jquery