/* STL 最大堆、最小堆的应用 */
#include <iostream>
#include <vector>
#include <algorithm> // using namespace std; /*
STL 堆操作
(1)make_heap()构造堆
void make_heap(first_pointer,end_pointer,compare_function);
默认比较函数是(<),即最大堆。
函数的作用是将[begin,end)内的元素处理成堆的结构 (2)push_heap()添加元素到堆
void push_heap(first_pointer,end_pointer,compare_function);
新添加一个元素在末尾,然后重新调整堆序。该算法必须是在一个已经满足堆序的条件下。
先在vector的末尾添加元素,再调用push_heap (3)pop_heap()从堆中移出元素
void pop_heap(first_pointer,end_pointer,compare_function);
把堆顶元素取出来,放到了数组或者是vector的末尾。
要取走,则可以使用底部容器(vector)提供的pop_back()函数。
先调用pop_heap再从vector中pop_back元素 (4)sort_heap()对整个堆排序
排序之后的元素就不再是一个合法的堆了。
*/ //最大堆
struct MaxHeapCmp
{
inline bool operator()(const int &x,const int &y)
{
return x < y;
}
}; //最小堆
struct MinHeapCmp
{
inline bool operator()(const int &x, const int &y)
{
return x > y;
}
}; void test()
{
std::vector<int> data{ ,,,,,,, }; //堆排序
std::make_heap(data.begin(), data.end(), MinHeapCmp());
for (int n : data)
{
cout << n << endl;
} printf("------------------\n"); //添加元素
data.push_back();
//再次堆排序
std::push_heap(data.begin(), data.end(), MaxHeapCmp());
for (int n : data)
{
cout << n << endl;
}
} int main()
{
test();
printf("-ok-\n");
getchar(); return ;
}

C++ STL堆操作的更多相关文章

  1. 堆以及stl堆的使用

    概念 性质: 1.堆是一颗完全二叉树,用数组实现.    2.堆中存储数据的数据是局部有序的. 最大堆:1.任意一个结点存储的值都大于或等于其任意一个子结点中存储的值.      2.根结点存储着该树 ...

  2. STL 常见操作

    stl的操作不是很熟练, 记录一下 1.vector: 排序: sort(vc.begin(),vc.end()); 去重: sort(vc.begin(),vc.end()); num.erase( ...

  3. STL 堆

    洛谷P3378 [模板]堆 #include <iostream> #include <cstdio> #include <algorithm> #include ...

  4. STL 堆的使用

    本来是要写leetcode上的merge k sorted lists那道题目,这个题目我还是很熟悉的,毕竟是看过算法导论的人,但是写的过程中对堆的维护代码还是挺多的,所以我想到了STL中的堆.下面就 ...

  5. 堆操作,malloc

    PS:堆空间缺省值都是cd,栈空间缺省值都是cc 内存有四区:栈.全局(静态).常量.除此以外的空间暂时不能随意使用,但是通过malloc函数申请就可以使用了. 利用malloc申请一个int变量,注 ...

  6. [算法]用java实现堆操作

    问题描述:(1)建堆:将数组A[1..n]变成一个最大堆.(课本6.3)(2)堆排序:将一个堆中的元素按递减排序输出.(3)用插入方法建堆:堆大小从1到n每次插入一个元素到堆中,直到n个元素入堆.(课 ...

  7. bzoj1293: [SCOI2009]生日礼物(stl堆)

    1293: [SCOI2009]生日礼物 题目:传送门 题解: 据说这道题乱搞随便就水过了 本蒟蒻想到了一个用堆的水法(还专门学了学queue): 如果把每一种颜色的下一个位置都记录一下的话,一开始就 ...

  8. 【转载】 C++ stl string 操作

        总结一下C++中string的操作,来自〈C++ Primer〉第四版. 1. string对象的定义和初始化: 12345678910111213 string s1; //空串string ...

  9. STL容器操作

    目录 1. 数组 2. Vector 3. List 3.1. std::forward_list 4. Tuple 4.1. 运行期索引 4.2. 元组合并 4.3. 元祖遍历 5. Pair 6. ...

随机推荐

  1. Python——lambda函数

    Lambda 函数又称匿名函数,匿名函数就是没有名字的函数,函数没有名字也行?当然可以啦.有些函数如果只是临时一用,而且它的业务逻辑也很简单时,就没必要非给它取个名字不可. 好比电影里面的群众演员,往 ...

  2. php 生成随机字符串

    /**     * 获取随机字符串     * @param $lenth     * @return string     */     function getRandStr($lenth = 2 ...

  3. CentOs中玩docker

    1.启动: systemctl start docker.service 2.停止: systemctl stop docker 3.从usts上拉取仓库 编辑文件 vi /etc/docker/da ...

  4. Ubuntu编译安装配置Redis以及基本使用

    1.首先下载redis curl -O http://download.redis.io/releases/redis-4.0.8.tar.gz 2.解压压缩包 .tar.gz 3.安装TCL测试工具 ...

  5. C/JS_实现选择排序

    1.js var arr = prompt("请输入一个数组(以“,”隔开):").split(",").map(function(data){ return ...

  6. SSD固态硬盘测试工具收集(持续更新)

    https://www.crsky.com/zhuanti/gutaiyingpanceshi.html https://www.crsky.com/zhuanti/ssdjiance.html ht ...

  7. Cocos Creator脚本开发事例

    HelloWorld.js window.Global = { gint: 168, }; cc.Class({ extends: cc.Component, properties: { label: ...

  8. bootstrap-3-上传图片-列表显示

    效果 导入的js和css <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href=" ...

  9. 使用wrk进行压力测试

    最近需要对新的服务进行压力测试.比较了ab和jemeter以及wrk.最终选择wrk来作为压力测试工具,可以把cpu压到100%. 官方源码: https://github.com/wg/wrk 安装 ...

  10. ValueError: output parameter for reduction operation logical_and has too many dimensions ?

    https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.all.html#numpy.all 运行示例,却发生错误 import ...