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 ...
随机推荐
- OmniPlan,一款让你无法自拔的项目管理工具(仅适用于MAC系统)
OmniPlan 大家都知道Windows系统可以使用project来方便的管理跟踪项目的各项任务,那么Mac系统有没有这样专业级的项目管理软件了?答案是有,今天给大家推荐的Mac系统下的项目管理 ...
- error2019-01-17 宏STDOUT_FILENO
STDOUT_FILENO定义在<unistd.h> EXIT_SUCCESS <stdlib.h> 1.fatal error: sys/capability.h: No s ...
- error:crosses initialization of ...
switch(c) { case 0x01: int temp = a + b; .... break; case 0x02: break; default:break; } 此时会报如题所示错误 原 ...
- Myeclipse启动报错:An error has occurred.See the log file
出现这个问题是因为断电前myeclipse还在运行,日志报错如下: !ENTRY org.eclipse.osgi 4 0 2017-07-24 08:29:48.485 !MESSAGE An er ...
- poj 2229 Sumsets(记录结果再利用的DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 将一个数N分解为2的幂之和共有几种分法? 题解: 定义dp[ i ]为数 i 的 ...
- dos下编译java
dos下运行java程序,不借助其他的IDE,即可编译运行java程序. 工具/原料 电脑 方法/步骤 打开开始,运行cmd,进入dos界面. 分别运行java,和javac,检测jd ...
- Linux上svn搭建
安装svn yum -y install subversion 2.创建版本库 svnadmin create /home/svn/test 3.配置用户 vim /home/svn/test/co ...
- HTML学习笔记Day7
一.position定位属性,检索对象的定位方式 1.语法:{position:static(无特殊定位)/absolute(绝对定位)/relative(相对定位)/fixed(固定定位):} 1) ...
- mybatis下载地址(所有版本)
https://github.com/mybatis/mybatis-3/releases,这个github里面几乎包含了所有的没有batis
- 剑指Offer_编程题_16
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. /* struct ListNode { int val; struct ListNode *n ...