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 ...
随机推荐
- Mock1 moco框架的基本介绍
前言: Mock就是模拟接口的,一般在开发人员还没有开发完接口,但是有接口文档,这个时候就可以执行接口测试,前端同学也可以用mock功能给自己使用. 功能:可以模拟http协议发送请求 下载链接:ht ...
- maomao的fft板子
\(QwQ\) #include <cmath> #include <cstdio> #include <cstring> #include <iostrea ...
- 微信小程序:分页和加载更多
直接上代码吧.不足之处,多多指教,一起进步 1.wxml页面的最后敲上,css自己定义 <view class="loadmore" mtype="{{mtype} ...
- mysql中using
select * from ( SELECT u.utm_source ,count(DISTINCT u.mobile) as new_user -- 登记用户 FROM 表名 u WHERE u. ...
- linux crontab定时任务不执行
如crontab 没有成功,检测crontab 服务是否启动, /etc/init.d/crond status 查看crond状态 /etc/init.d/crond restart 重启crond ...
- List数组
大家好,我是蜀云泉.我的博文之中存在的不足之处希望大家包涵. 今天学习unity时,在实现某个功能的脚本中发现了List数组.关于List数组的问题我在学C#时已经接触了一点,但是我比较粗心和浮躁以前 ...
- Linux防火墙开放端口
# vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT -A INPUT - ...
- 使用Ruby处理大型CSV文件
处理大型文件是一种内存密集型操作,可能导致服务器耗尽RAM内存并交换到磁盘.让我们看一下使用Ruby处理CSV文件的几种方法,并测量内存消耗和速度性能. Prepare CSV data sample ...
- 前端基础之JQuery - day15
写在前面 上课第15天,打卡: 张国臂掖,以通西域: ########### # 课上简书 # ########## http://jquery.cuishifeng.cn/index.html JQ ...
- SpringBoot系列: Java应用程序传参和SpringBoot参数文件
===========================向java 程序传参的几种形式:===========================1. 使用 OS 环境变量. 这个不推荐. 2. 使用JVM ...