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 ...
随机推荐
- django基于中间件的IP访问频率控制
一.中间件的代码 注意:成功时返回的是None,那样才会走视图层,返回httpresponse就直接出去了 import time from django.utils.deprecation impo ...
- 简单ATM系统
模拟实现一个ATM + 购物商城程序1.额度 15000或自定义2.实现购物商城,买东西加入 购物车,调用信用卡接口结账3.可以提现,手续费5%4.每月22号出账单,每月10号为还款日,过期未还,按欠 ...
- google chrome 浏览器书签丢失问题
在一次新打开标签页时,电脑卡死,强制重启后打开google chrome 浏览器,发现历史什么的都在,但书签栏全部丢失了 找到 系统盘:\Users\用户名\AppData\Local\Google\ ...
- chrome截图全网页
1.F12 2.ctrl+shift+p 3.输入:capture 4.选择Capture full size screenshot
- CatBoost算法和调参
欢迎关注博主主页,学习python视频资源 sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?co ...
- docker 基础之网络管理
docker网络基础. docker使用到的与linux网络有关的主要技术 Network Namespace(网络命名空间) Veth设备对 Iptables/NetFilter 网桥 路由 标准的 ...
- saltstack API(一) 安装并测试
python3 安装api # 首先安装python3 .tgz cd Python- . ./configure make make install mv /usr/bin/python /usr/ ...
- 修复./mysql/proc
mysql数据库只能建不能删的错误提示及处理方法: mysql> drop database zabbixaa; ERROR 145 (HY000): Table ‘./mysql/proc‘ ...
- java io系列24之 BufferedWriter(字符缓冲输出流)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_24.html 更多内容请参考:java io系列01之 "目录" Buffere ...
- 设计模式---数据结构模式之职责链模式(Chain of Responsibility)
一:概念 职责链模式(CoR,Chain of Responsibility)是行为模式之一,该模式构造一系列分别担当不同的职责的类的对象来共同完成一个任务,这些类的对象之间像链条一样紧密相连,所以被 ...