STL之heap学习
C++标准库中的堆-heap
make_heap函数,包括两个参数(begin(number),end(number)).(左闭右开)
pop_heap函数,包括两个参数,起始位置和终止位置,将当前区间的首位(也就是当前区间最大放在)end的位置。
push_heap函数,包括两个参数,起始位置和终止位置,将当前区间的最后一个元素插入到堆中。
sort_heap函数,包括两个参数,起始位置和终止位置,多次调用make_heap函数和pop_heap函数,实现最终对整个区间进行排序(注意调用之前需要先调用一次make_heap函数)。
is_heap函数,包括两个参数,起始位置和终止位置,判断当前的区间是否满足大顶堆,如果满足输出1,否则输出0。
is_heap_until函数,返回的是第一个不满足二叉堆的迭代器。
第一个参数代表起始位置,第二个参数代表结束位置。
每一次调用都是把当前区间内的最大值放在这个区间的最前面,就是堆排序的感觉。然后每排完一次序,我们可以将头部元素弹出。库函数 pop_head(begin(number),end(number)).这个时候,最大值被移动到了end的位置。
示例代码:
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
int a[maxn];
vector<int>q;
int main(){
for(int i=;i<=;i++){
q.push_back(i);
}
int n=;
for(int i=;i<=;i++){
make_heap(q.begin(),q.begin()+q.size()-n);
for(int j=;j<q.size();j++){
if(j==)cout<<q[j];
else cout<<" "<<q[j];
}
cout<<endl;
pop_heap(q.begin(),q.begin()+q.size()-n);
n++;
}
return ;
}
vector
sort_heap函数。
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
int a[maxn];
vector<int>q;
int main()
{
for(int i=; i<=; i+=)
{
q.push_back(i);
}
// make_heap(q.begin(),q.begin()+q.size());
q.push_back();
make_heap(q.begin(),q.end());
for(int j=; j<q.size(); j++)
{
if(j==)
cout<<q[j];
else
cout<<" "<<q[j];
}
cout<<endl;
sort_heap(q.begin(),q.end());
for(int j=; j<q.size(); j++)
{
if(j==)
cout<<q[j];
else
cout<<" "<<q[j];
}
cout<<endl;
return ;
}
is_heap函数。
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
int a[maxn];
vector<int>q;
int main()
{
for(int i=; i<=; i+=)
{
q.push_back(i);
}
for_each(q.begin(),q.end(),[](int i){cout<<" "<<i<<endl;});
// make_heap(q.begin(),q.begin()+q.size());
q.push_back();
make_heap(q.begin(),q.end());
sort_heap(q.begin(),q.end());
int flag=is_heap(q.begin(),q.end());
cout<<flag<<endl;
return ;
}
is_heap_until函数。
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
int a[maxn];
vector<int>q;
int main()
{
for(int i=; i<=; i+=)
{
q.push_back(i);
}
for_each(q.begin(),q.end(),[](int i){cout<<" "<<i<<endl;});
// make_heap(q.begin(),q.begin()+q.size());
q.push_back();
// make_heap(q.begin(),q.end());
// sort_heap(q.begin(),q.end());
auto flag=is_heap_until(q.begin(),q.end());
cout<<*flag<<endl;
return ;
}
小总结:
pop_heap,push_heap,sort_heap函数都需要在make_heap函数的前提下使用。
STL之heap学习的更多相关文章
- L2-012. 关于堆的判断(STL中heap)
L2-012. 关于堆的判断 将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x and y ...
- stl源码学习(版本2.91)--list
stl源码学习(版本2.91)--list 一,阅读list()构造函数的收获 1,默认构造函数的作用和被调用的时机 struct no{ no(int i){} //no(){ // std::co ...
- C++STL标准库学习笔记(三)multiset
C++STL标准库学习笔记(三)multiset STL中的平衡二叉树数据结构 前言: 在这个笔记中,我把大多数代码都加了注释,我的一些想法和注解用蓝色字体标记了出来,重点和需要关注的地方用红色字体标 ...
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- 【STL源码学习】STL算法学习之三
第一章:前言 数量不多,用到的时候会很爽. 第二章:明细 STL算法中的又一个分类:分割:将已有元素按照既定规则分割成两部分. is_partitioned 函数原型: template <c ...
- 【STL源码学习】STL算法学习之二
第一章:前言 学习笔记,记录学习STL算法的一些个人所得,在以后想用的时候可以快速拾起. 第二章:明细 copy 函数原型: template <class InputIterator, cla ...
- 【STL源码学习】STL算法学习之一
第一章:引子 STL包含的算法头文件有三个:<algorithm><numeric><functional>,其中最大最常用的是<algorithm>, ...
- 【STL源码学习】细品vector
第一节:vector简介 vector是一种典型的类模板,使用的时候必须进行实例化. vector的数据存储在数组上,支持随机访问迭代器,支持下标操作[]和at操作,支持手动扩容和自动容量增长. ve ...
随机推荐
- 数位DP+其他
参考资料: [1]:数位dp总结 之 从入门到模板 [2]:浅谈数位DP 题目一览表 来源 考察知识点 A 4352 "XHXJ's LIS" hdu 数位DP+状压DP+LIS ...
- 互联网运营+SEO:推荐必看的5本书籍
本文首发于:风云社区(scoee.com) 最近开始学习和研究互联网运营和SEO,对于我这个小白来讲,还是有些吃力,毕竟从来没接触这方面的,尽管在之前的软件公司做过售前和产品相关的工作,但毕竟与互联网 ...
- 5款 Mac 常用PDF阅读和编辑软件推荐
PDF和Word.TXT等文档一样,都是我们最常用的文档格式,那么一款好用的浏览或编辑PDF的工具就很有必要了,今天和大家分享5款Mac上优秀的PDF阅读和编辑工具. 以下内容来自[风云社区 SCOE ...
- Go-day01
Go-实现hello_world package main import "fmt" func main() { fmt.Println("hello world!&qu ...
- Event Recommendation Engine Challenge分步解析第七步
一.请知晓 本文是基于: Event Recommendation Engine Challenge分步解析第一步 Event Recommendation Engine Challenge分步解析第 ...
- ipython介绍及使用
1. IPython介绍 ipython是一个python的交互式shell,比默认的python shell好用得多,支持变量自动补全,自动缩进,支持bash shell命令,内置了许多很有用的功能 ...
- hibernate validator【原】
hibernate validator 功能 在开发中经常做一些字段校验的功能,比如非空,长度限制,邮箱验证等等,为了省掉这种冗长繁琐的操作,hibernate validator提供了一套精简的注释 ...
- Spark源码剖析 - SparkContext的初始化(九)_启动测量系统MetricsSystem
9. 启动测量系统MetricsSystem MetricsSystem使用codahale提供的第三方测量仓库Metrics.MetricsSystem中有三个概念: Instance:指定了谁在使 ...
- VM克隆后找不到eth0的问题解决
问题描述 使用VM WorkStation新建虚拟机A,查看IP信息,显示结果: [root@centos65x64 ~]# ifconfig -a eth0 Link encap:Ethernet ...
- Install Ubuntu Server
进入引导程序以后, 选择Install Ubuntu Server, 安装主菜单如下: 依次配置: 接着 https://www.youtube.com/watch?v=gqLaT01yei0