make_heap原型:

std::make_heap

default (1)
template <class RandomAccessIterator>
void make_heap (RandomAccessIterator first, RandomAccessIterator last);
custom (2)
template <class RandomAccessIterator, class Compare>
void make_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp );

该函数是使用范围内的元素建立成一个堆(默认是大顶堆)。

并将堆存放到原来的容器内。

将范围内的元素建成堆能够高速地取得其范围内的最大值,而且支持高速插入元素。

一个简单的样例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void makeheap(){
vector<int> vi{1,7,5,6,8,9,3};
cout<<"at first vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
make_heap(vi.begin(),vi.end());
cout<<"after make_heap(vi.begin(),vi.end())\nvi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl; }

执行结果:



看到假设将其画成一个堆的变化那就是

sort_heap原型:

std::sort_heap

default (1)
template <class RandomAccessIterator>
void sort_heap (RandomAccessIterator first, RandomAccessIterator last);
custom (2)
template <class RandomAccessIterator, class Compare>
void sort_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp);

该函数事实上就是对堆进行一个排序。

使用operator<进行比較,前提是范围内的元素已经是一个堆了,否则会出错!

排序后序列将失去堆的属性。

一个简单的样例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void sortheap(){
vector<int> vi{1,7,5,6,8,9,3};
cout<<"at first vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
make_heap(vi.begin(),vi.end());
cout<<"after make_heap(vi.begin(),vi.end())\nvi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
sort_heap(vi.begin(),vi.end());
cout<<"after sort_heap(vi.begin(),vi.end())\nvi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl; }

执行结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4NDQzNTIxNTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,能够在以下留言或者点击左上方邮件地址给我发邮件。指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-17

于GDUT

——————————————————————————————————————————————————————————————————


STL algorithm算法make_heap和sort_heap(32)的更多相关文章

  1. STL algorithm算法merge(34)

    merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...

  2. STL algorithm算法mismatch(37)

    mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...

  3. STL algorithm算法is_permutation(27)

    is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...

  4. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  5. STL algorithm算法minmax,minmax_element(36)

    minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...

  6. STL algorithm算法min,min_element(35)

    min样板: std::min C++98 C++11 C++14 default (1) template <class T> const T& min (const T& ...

  7. STL algorithm算法max,max_elements(33)

    max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...

  8. STL algorithm算法mov,move_backward(38)

    move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...

  9. STL algorithm算法lexicographical_compare(30)

    lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...

随机推荐

  1. 阿里云 django的一次web维护记录

    首先, 丢给我一个阿里云的server的账号/password,之前没有玩过阿里云,想想应该也是ssh服务来远程登陆. 环境: centos+nginx+uwsgi+python2.7+django. ...

  2. Oracle Sqlplus中上下键出现^[[A乱码问题

    安装rlwrap  下载:http://utopia.knoware.nl/~hlub/uck/rlwrap/ 或者 百度云盘:http://pan.baidu.com/s/1ntM8YXr 须要先安 ...

  3. Zabbix 监控搭建

    Zabbix官网地址:https://www.zabbix.com/download 1.服务端 1.操作前安装好Mysql数据库 配置yum源,安装部署Zabbix rpm -i http://re ...

  4. HDU——T 1576 A/B

    http://acm.hdu.edu.cn/showproblem.php?pid=1576 Time Limit: 1000/1000 MS (Java/Others)    Memory Limi ...

  5. 洛谷 P1130 红牌

    P1130 红牌 题目描述 某地临时居民想获得长期居住权就必须申请拿到红牌.获得红牌的过程是相当复杂 ,一共包括N个步骤.每一步骤都由政府的某个工作人员负责检查你所提交的材料是否符合条件.为了加快进程 ...

  6. Caused by: java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.getServletContext()L

    在做项目的时候,出现Caused by: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletCo ...

  7. COCOS学习笔记--持续动作ActionInterval

    上一篇博客介绍了即时动作ActionInstant.与即时动作相对的是持续动作ActionInterval. 顾名思义,持续动作就是须要一段时间来持续运行的动作,而且在有限时间内改变运行对象的一些属性 ...

  8. RAC RMAN 备份 RMAN-03009 ORA-19504 ORA-27040 RMAN-06012 channel c3 not allocated 错误分析

    备份Shell 脚本如下: ######################################################################## ## RAC_hot_da ...

  9. LeetCode Algorithm 03_Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  10. angular6添加material-svgIcon

    1. app/assets/util/util.svg.ts 统一管理svg字体库,避免各个模块分散加载.所以使用公共文件统一处理 再到core.module.ts中引入.在core模块下的所有组价都 ...