make_heap()等函数的用法
1.make_heap()
make_heap()用于把一个可迭代容器变成一个堆,默认是大顶堆。
它有三个参数。第一个参数是指向开始元素的迭代器,第二个参数是指向最末尾元素的迭代器,第三个参数是less<>()或是greater<>(),前者用于生成大顶堆,后者用于生成小顶堆,第三个参数默认情况下为less<>(),less<int>()用于生成大顶堆。
要使用less<int>(),以及greater<int>(),请添加头文件#include <functional>,且一定要加括号less<>()
举例:
#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>
using namespace std;
int main(){
vector<int> q;
for (int i = 0; i < 10; i++) {
q.push_back(i);
}
make_heap(q.begin(), q.end(),less<int>());
for (int i = 0; i < q.size(); i++) {
cout << q[i] << " ";
}
return 0;
}
q在完成初始化后,make_heap()使其满足了大顶堆的特性。
2.pop_heap()
pop_heap()用于将堆的第零个元素与最后一个元素交换位置,然后针对前n - 1个元素调用make_heap()函数,它也有三个参数,参数意义与make_heap()相同,第三个参数应与make_heap时的第三个参数保持一致。
注意:pop_heap()函数,只是交换了两个数据的位置,如果需要弹出这个数据,请记得在pop_heap()后加上
q.pop_back();
例子:
#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>
using namespace std;
void display(vector<int>q) {
for (int i = 0; i < q.size(); i++) {
cout << q[i] << " ";
}
cout << endl;
}
int main(){
vector<int> q;
for (int i = 0; i < 10; i++) {
q.push_back(i);
}
make_heap(q.begin(), q.end(),less<int>());
display(q);
pop_heap(q.begin(), q.end(), less<int>());
display(q); return 0;
}
3.push_heap()
push_heap()用于把数据插入到堆中,它也有三个参数,其意义与make_heap()的相同,第三个参数应与make_heap时的第三个参数保持一致。
在使用push_heap()前,请确保已经把数据通过q.push_back()传入q中,而不是在push_heap()后再使用q.push_back(t)!!
例子:
#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>
using namespace std;
void display(vector<int>q) {
for (int i = 0; i < q.size(); i++) {
cout << q[i] << " ";
}
cout << endl;
}
int main(){
vector<int> q;
for (int i = 0; i < 10; i++) {
q.push_back(i);
}
make_heap(q.begin(), q.end(),less<int>());
cout << "插入前" << endl;
display(q);
q.push_back(12);
push_heap(q.begin(), q.end(), less<int>());
cout << "插入后" << endl;
display(q);
return 0;
}
4.sort_heap()
sort_heap()是将堆进行排序,排序后,序列将失去堆的特性(子节点的键值总是小于或大于它的父节点)。它也具有三个参数,参数意义与make_heap()相同,第三个参数应与make_heap时的第三个参数保持一致。大顶堆sort_heap()后是一个递增序列,小顶堆是一个递减序列。
请在使用这个函数前,确定序列符合堆的特性,否则会报错!
#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>
using namespace std;
void display(vector<int>q) {
for (int i = 0; i < q.size(); i++) {
cout << q[i] << " ";
}
cout << endl;
}
int main(){
vector<int> q;
for (int i = 0; i < 10; i++) {
q.push_back(i);
}
make_heap(q.begin(), q.end(),less<int>());
cout << "sort前" << endl;
display(q);
sort_heap(q.begin(), q.end(), less<int>());
cout << "sort后" << endl;
display(q);
return 0;
}
make_heap()等函数的用法的更多相关文章
- 有关日期的函数操作用法总结,to_date(),trunc(),add_months();
相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...
- Oracle to_date()函数的用法
Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...
- js中bind、call、apply函数的用法
最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...
- Oracle trunc()函数的用法
Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...
- freemarker内置函数和用法
原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...
- matlab中patch函数的用法
http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...
- JavaScript中常见的数组操作函数及用法
JavaScript中常见的数组操作函数及用法 昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下 ...
- JavaScript中常见的字符串操作函数及用法
JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
- oracle的substr函数的用法
oracle的substr函数的用法 取得字符串中指定起始位置和长度的字符串 substr( string, start_position, [ length ] ) 如: substr( ...
随机推荐
- node安装问题
这个是我碰到的 这是解决方法,祝你好运
- bzoj1619
题解: 简单灌水 从最高的开始 代码: #include<bits/stdc++.h> ; typedef long long ll; using namespace std; ]={,, ...
- bzoj1625
题解: 简单dp 要一维 代码: #include<bits/stdc++.h> using namespace std; ; int n,m,a[N],b[N],f[N]; int ma ...
- python *args **kwargs,传入不固定的参数给函数,或者传入很多的内容给函数,常用在构造函数中。
''' 例1:展示*args的用法,传入多个参数,不进行预先定义. 本例传入了3个参数.没有预先定义.在函数内自动生成元组() ''' def q1(*args): print('例1') print ...
- React Native笔记整理
判断一个APP页面时原生还是H5:http://www.cnblogs.com/sonice-cinsy/p/5671324.html 写给移动开发者的React Native指南:http://bl ...
- NodeJS 难点(网络,文件)的 核心 stream 三:readable ?
什么是可读流 可读流 常见 读取磁盘文件.读取网络请求内容等,看一下前面介绍什么是流用的例子: const rs = fs.createReadStream(filePath); 我们常见的控 ...
- jquery 实现内容的级联选取
- php 加载字体 并保存成图片
// Set the content-type header("Content-type: image/png"); // Create the image $im = image ...
- CTF之当铺密码
当铺密码即:汉字的笔画有几笔出头,则代表数字几 例如:由=1 王=6 大=5
- 租酥雨的NOIP2018赛前日记
租酥雨的NOIP2018赛前日记 离\(\mbox{NOIP2018}\)只剩下不到一个月的时间辣! 想想自己再过一个月就要退役了,觉得有必要把这段时间的一些计划与安排记录下来. 就从国庆收假开始吧. ...