博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL中的nth_element()方法的使用
阅读量:7075 次
发布时间:2019-06-28

本文共 1576 字,大约阅读时间需要 5 分钟。

STL中的nth_element()方法的使用 通过调用nth_element(start, start+n, end)

方法可以使第n大元素处于第n位置(从0开始,其位置是下标为
n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的,下面是这个方法的具体使用方法.

1 #include 
2 3 #include
4 5 #include
6 7 #include
8 9 using namespace std;10 11 12 13 int main()14 15 {16 17 const int VECTOR_SIZE = 50 ;18 19 20 21 vector
Numbers(VECTOR_SIZE) ;22 23 24 25 vector
::iterator start, end, it ;26 27 28 29 // Initialize vector Numbers30 31 for(int i=0;i<50;++i){32 33 Numbers[i]=i;34 35 }36 37 /*由于赋值时是有序的,下面random_shuffle()方法将这些数据的顺序打乱*/38 39 random_shuffle(Numbers.begin(),Numbers.end());40 41 42 43 // location of first element of Numbers44 45 start = Numbers.begin() ; 46 47 48 49 // one past the location last element of Numbers50 51 end = Numbers.end() ; 52 53 54 55 cout << "Before calling nth_element/n" << endl ;56 57 58 59 // print content of Numbers60 61 cout << "Numbers { " ;62 63 for(it = start; it != end; it++)64 65 cout << *it << " " ;66 67 cout << " }/n" << endl ;68 69 70 71 /* 72 73 * partition the elements by the 8th element,74 75 *(notice that 0th is the first element)76 77 */ 78 79 nth_element(start, start+8, end) ;80 81 82 83 cout << "After calling nth_element/n" << endl ;84 85 86 87 cout << "Numbers { " ;88 89 for(it = start; it != end; it++)90 91 cout << *it << " " ;92 93 cout << " }/n" << endl ;94 95 system("pause");96 97 }

 

转载于:https://www.cnblogs.com/ECJTUACM-873284962/p/6741498.html

你可能感兴趣的文章
flask 第七章 简陋版智能玩具 +MongoDB初识和基本操作
查看>>
Maven之setting.xml 配置详解
查看>>
linux中运行.sql文件
查看>>
ftl 列表弄成js数组
查看>>
课后作业:字串加密
查看>>
REGEXP 正则的实现两个字符串组的匹配。(regexp)
查看>>
python爬虫之登录
查看>>
nginx的proxy_pass路径转发规则最后带/问题
查看>>
javascript访问加runat="server" 的Html控件的方法
查看>>
JS特效,将左边项移动到右边
查看>>
七牛云:ckeditor JS SDK 结合 C#实现多图片上传。
查看>>
CORS FOR AspNetCore
查看>>
iOS—仿微信单击放大图片
查看>>
noteexpress使用指南
查看>>
C从控制台(stdin)输入带空格的字符串到字符数组中
查看>>
Codeforces Round #428 A. Arya and Bran【模拟】
查看>>
【设计模式】抽象工厂模式
查看>>
OO第四次博客
查看>>
面试STAR法则
查看>>
很反感
查看>>