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()等函数的用法的更多相关文章

  1. 有关日期的函数操作用法总结,to_date(),trunc(),add_months();

    相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...

  2. Oracle to_date()函数的用法

    Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...

  3. js中bind、call、apply函数的用法

    最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...

  4. Oracle trunc()函数的用法

    Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...

  5. freemarker内置函数和用法

    原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...

  6. matlab中patch函数的用法

    http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...

  7. JavaScript中常见的数组操作函数及用法

    JavaScript中常见的数组操作函数及用法 昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下 ...

  8. JavaScript中常见的字符串操作函数及用法

    JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...

  9. oracle的substr函数的用法

    oracle的substr函数的用法 取得字符串中指定起始位置和长度的字符串   substr( string, start_position, [ length ] ) 如:     substr( ...

随机推荐

  1. 快速切题 hdu2416 Treasure of the Chimp Island 搜索 解题报告

    Treasure of the Chimp Island Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  2. [置顶]使用scrapy_redis,自动实时增量更新东方头条网全站新闻

    存储使用mysql,增量更新东方头条全站新闻的标题 新闻简介 发布时间 新闻的每一页的内容 以及新闻内的所有图片.项目文件结构. 这是run.py的内容 1 #coding=utf-8 2 from ...

  3. Flask初级(八)flash与前台交互get post 简介

    Project name :Flask_Plan templates:templates static:static 两种 HTTP 请求方法:GET 和 POST在客户机和服务器之间进行请求-响应时 ...

  4. 《Python》 函数嵌套、闭包和迭代器

    一.函数的嵌套: 1.函数的嵌套调用 def max2(x,y): m = x if x>y else y return m def max4(a,b,c,d): res1 = max2(a,b ...

  5. 使用pool的多进程,不执行的问题

    from multiprocessing import Pool def fetch_data(idlist,test): pass p=Pool(4) result=[] for i in rang ...

  6. 打开TestFlight并链接到具体的应用下载界面

    在我们应用正式上线前,许多时候都会有小流量的测试,选取部分核心用户抢先试用,在TestFlight出来之前,一般都是通过企业证书打包,然后提供给用户下载地址,安装测试使用,这个有个不爽之处就是,企业i ...

  7. 使用MyEclipse开发Java EE应用:EJB项目开发初探(下)

    你开学,我放价!MyEclipse线上狂欢继续!火热开启中>> [MyEclipse最新版下载] 三.EJB 3.x项目中的持久性支持 当创建EJB 3.x项目时,作为选项您可以添加JPA ...

  8. 教你如何打开android4.3和4.4中隐藏的AppOps

    注:下面的方法在4.4.2更新后已失效! PreferenceActivity的switchToHeaderInner()函数中会调用isValidFragment函数来检查fragment是否合法. ...

  9. 全球DEM数据GMT绘制

    http://gmt-china.org/download/

  10. SharePoint 服务应用程序管理-PowerShell

    1. 安装所有可用的服务应用程序 Install-SPService -Provision 2. 显示场中所有可用的服务应用程序 Get-SPServiceApplication 3. 获取指定的服务 ...