堆不是一中sort ranges,堆中的元素不会以递增方式排列,内部以树状形式排列,该结构以每个结点小于等于父节点构成,优先队列就是以堆来实现

make_heap

//版本一:用operator <比较元素
template <class RandomAccessIterator>
void make_heap(RandomAccessIterator first,RandomAccessIterator last);

//版本二:用自定义的function object比较元素
template <class RandomAccessIterator,class StrictWeakOrdering>
void make_heap(RandomAccessIterator first,RandomAccessIterator last,StrictWeakOrdering cmp);

push_heap

//版本一:用operator <比较元素
template <class RandomAccessIterator>
void push_heap(RandomAccessIterator first,RandomAccessIterator last);

//版本二:用自定义的function object比较元素
template <class RandomAccessIterator,class StrictWeakOrdering>
void push_heap(RandomAccessIterator first,RandomAccessIterator last,StrictWeakOrdering cmp);

  堆以[first,last-1)表示,新增加的元素为*(last-1);[first,last-1)为有效地range,也就是其不为空并且是一个堆

pop_heap

//版本一:用operator <比较元素
template <class RandomAccessIterator>
void pop_heap(RandomAccessIterator first,RandomAccessIterator last);

//版本二:用自定义的function object比较元素
template <class RandomAccessIterator,class StrictWeakOrdering>
void pop_heap(RandomAccessIterator first,RandomAccessIterator last,StrictWeakOrdering cmp);

  从堆中移除最大元素(即*first);[first,last-1)为有效地range,也就是其不为空;这两个版本的操作都有一个必然的结果:从堆中移除的元素是*(last-1),并且移除后为一个新堆

sort_heap

//版本一:用operator <比较元素
template <class RandomAccessIterator>
void sort_heap(RandomAccessIterator first,RandomAccessIterator last);

//版本二:用自定义的function object比较元素
template <class RandomAccessIterator,class StrictWeakOrdering>
void sort_heap(RandomAccessIterator first,RandomAccessIterator last,StrictWeakOrdering cmp);

  把堆转化为一个sorted ranges,不一定会保持等价元素的相对顺序

is_heap

//版本一:用operator <比较元素
template <class RandomAccessIterator>
void is_heap(RandomAccessIterator first,RandomAccessIterator last);

//版本二:用自定义的function object比较元素
template <class RandomAccessIterator,class StrictWeakOrdering>
void is_heap(RandomAccessIterator first,RandomAccessIterator last,StrictWeakOrdering cmp);

  测试一个序列是否是一个堆

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    vector<,,,,,,};
    make_heap(v.begin(),v.end()-);
    for_each(v.begin(),v.end(),[](int i)
    {
        cout<<i<<" ";
    });
    cout<<endl;

    push_heap(v.begin(),v.end());
    for_each(v.begin(),v.end(),[](int i)
    {
        cout<<i<<" ";
    });
    cout<<endl;

    cout<<is_heap(v.begin(),v.end())<<endl;

    sort_heap(v.begin(),v.end());
    cout<<is_heap(v.begin(),v.end())<<endl;
    ;
}

堆的操作(make_heap,push_heap,pop_heap,sort_heap,is_heap)的更多相关文章

  1. STL make_heap push_heap pop_heap sort_heap

    make_heap: default (1) template <class RandomAccessIterator> void make_heap (RandomAccessItera ...

  2. POJ - 1456 贪心 堆常用操作 注意细节

    题意:给定n个商品的deadline和profit,求每天卖一件的情况下的最大获利 显然是一道贪心 按deadline从小到大排序好,动态维护小根(profit)堆的大小<=当前deadline ...

  3. C#=> 栈模仿堆的操作

    //原理,利用两个栈,互相作用,来模仿堆的效果,先进先出.. using System; using System.Collections.Generic; using System.Linq; us ...

  4. 论C++STL源代码中关于堆算法的那些事

    关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比較靠上的O(nlogn)常常会拿来和高速排序和归并排序讨论,并且它还有个长处是它的空间复杂度为O(1), 可是STL中没有给我们提供 ...

  5. 不要重复发明轮子-C++STL

    闫常友 著. 中国铁道出版社. 2013/5 标准的C++模板库,是算法和其他一些标准组件的集合. . 1.类模板简介 2.c++中的字符串 3.容器 4.c++中的算法 5.迭代器 6.STL 数值 ...

  6. STL--STL和她的小伙伴们:

    STL--概述: 标准模板库(StandardTemplateLibrary,STL),是C++程序设计语言标准模板库.STL是由Alexander Stepanov.Meng Lee和David R ...

  7. (转)c++一些知识点

    异常详解: https://www.cnblogs.com/hdk1993/p/4357541.html#top 模版详解: https://blog.csdn.net/lezardfu/articl ...

  8. cb50a_c++_STL_算法_局部排序partial_sort

    cb50a_c++_STL_算法_局部排序partial_sort partial_sort(b,se,e)排序一部分,begin,source end,endcout << " ...

  9. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

随机推荐

  1. delete请求

    Action(){ int HttpRetCode; //定义一个变量,用于接收HTTP返回的状态码 web_add_header("Session-Id", "2e25 ...

  2. Map的isEmpty()与==null的区别

    isEmpty()方法判断Map是否有内容(即new分配空间后是否put键值对),若没有内容则true,否则false == null是判断map是否为null(即是否new分配空间,和其中的键值对没 ...

  3. window.setTimeout和window.setInterval的区别,及用其中一个方法记录时间。

    window.setTimeout(语句,时间)是在多久之后执行语句,语句只执行一次. window.setInterval(语句,时间)是每隔多久执行一次语句,语句循环执行. <!DOCTYP ...

  4. java中coroutine使用

    链接1:http://jm.taobao.org/2010/09/17/326/ 链接2:https://www.jianshu.com/p/0f1a6943eab5

  5. For all entries in

    Today I read about a blog explaining very detailedly on how to correctly use the key words FOR ALL E ...

  6. [转]Ubuntu安装Python3.6

    Ubuntu安装Python3.6   Ubuntu默认安装了Python2.7和3.5 输入命令python

  7. 2.21 JS处理滚动条

    2.21 JS处理滚动条 前言    selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了.常见场景:当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到 ...

  8. python使用traceback获取详细的异常信息

    原创来自:https://blog.csdn.net/mengtao0609/article/details/55049059 python使用traceback获取详细的异常信息 2017年02月1 ...

  9. Linux 对文件进行加密存放

    /********************************************************************** * Linux 对文件进行加密存放 * 说明: * Gi ...

  10. Ubuntu关闭进入screensaver模式

    /********************************************************************************* * Ubuntu关闭进入scree ...