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

 1 #include <iostream>
 2
 3 #include <algorithm>
 4
 5 #include <functional>
 6
 7 #include <vector>
 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<int> Numbers(VECTOR_SIZE) ;
22
23
24
25     vector<int>::iterator start, end, it ;
26
27
28
29     // Initialize vector Numbers
30
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 Numbers
44
45     start = Numbers.begin() ;
46
47
48
49  // one past the location last element of Numbers
50
51     end = Numbers.end() ;
52
53
54
55     cout << "Before calling nth_element/n" << endl ;
56
57
58
59   // print content of Numbers
60
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

STL中的nth_element()方法的使用相关推荐

  1. STL中 sort 排序方法

    头文件:<algorithm> 排序格式:sort (a,a+n)--  指从a[0]到a[n-1]排序,他有两种形式 (1)默认从小到大排序 (2)加cmp自定义排序方式:sort (a ...

  2. c++STL中的find()函数 有两种使用方法

    c++STL中的find()函数 有两种使用方法 方法一: 开头引头文件:中的函数 其调用形式为 find(start,end,value) start搜寻的起点,end搜寻的终点,要寻找的value ...

  3. C++ STL 迭代器在string类中的使用方法

    //以下例子说明迭代器在string类中的使用方法 #include <iostream> #include <string> #include <algorithm&g ...

  4. stl中各种容器的自定义比较函数

    class elem { public:elem();elem(int aa):a(aa){} public:int a;int getA(){return a;} };class elem1 { p ...

  5. C++ STL中的优先队列(priority_queue)使用

    原文:https://www.cnblogs.com/cielosun/p/5654595.html 今天讲一讲优先队列(priority_queue),实际上,它的本质就是一个heap,我从STL中 ...

  6. STL中基本容器有: string、vector、list、deque、set、map

    为什么80%的码农都做不了架构师?>>>    在STL中基本容器有: string.vector.list.deque.set.map set 和map都是无序的保存元素,只能通过 ...

  7. 关于STL中的map和hash_map

    在网上看到有关STL中hash_map的文章,以及一些其他关于STL map和hash_map的资料,总结笔记如下:     1.STL的map底层是用红黑树实现的,查找时间复杂度是log(n):   ...

  8. STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  9. C++ STL中vector用法简要总结

    1.基本使用举例 下面的代码给出了vector的基本使用举例. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...

最新文章

  1. 组合特征(四)linearsvm-tfidf(word)+lr-tfidf(article)
  2. WebX Qamp;A
  3. 借助JRebel使Tomcat支持热部署
  4. 第三章 Selenide测试框架(三)
  5. 【LeetCode】查找只出现一次的数字算法
  6. python 类和对象_面向对象的编程思想和Python的类,访问和属性,继承
  7. 鸿蒙OS扫描二维码,京东鸿蒙OS 2.0,京东鸿蒙OS 2.0手机app官方版预约 v1.0-手游汇
  8. Linux(ubuntu 18.0.4) Java环境安装,环境变量配置
  9. java json日期格式化_JSON格式化日期方法
  10. 计算机维修培训教材,计算机芯片级维修中心芯片级维修培训教材.pdf
  11. mysql学习记录_秦路天善智能Mysql学习笔记
  12. 与网友“阵春风”交流
  13. 【web可视化】canvas画图学习详细教程笔记完整代码
  14. docker_mysql
  15. 洛谷 P1095 守望者的逃离
  16. 5G智慧灯杆系统在智慧街区的应用
  17. Python顺序与选择结构
  18. ggplot报错 stat_count() can only have an x or y aesthetic
  19. 利用CAD提取高程点导出为TXT
  20. UE4迁移继承自C++的蓝图类(痛苦经历)

热门文章

  1. $@ 与 $* 差在哪?
  2. h3c 3600 acl 配置 滤源IP是网关的ARP报文的ACL规则
  3. 编译OpenSSH8.4的RPM包及升级
  4. liunx php redis扩展,CentOS 7下安装php-redis扩展及简单使用
  5. 用零知识证明解决投票安全
  6. 循环神经网络 递归神经网络_如何用递归神经网络预测空气污染
  7. CSS改变nth-child()和nth-last-child()的参数灵活选择元素编号
  8. 二分类任务:确定一个人是否年收入超过5万美元
  9. java培训机构如何选择适合自己的
  10. Sublime Text保存文件时自动去掉行末空格