STL algorithm算法make_heap和sort_heap(32)
make_heap原型:
std::make_heap
| default (1) |
template <class RandomAccessIterator> |
|---|---|
| custom (2) |
template <class RandomAccessIterator, class Compare> |
该函数是使用范围内的元素建立成一个堆(默认是大顶堆)。
并将堆存放到原来的容器内。
将范围内的元素建成堆能够高速地取得其范围内的最大值,而且支持高速插入元素。
一个简单的样例:
#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> |
|---|---|
| custom (2) |
template <class RandomAccessIterator, class Compare> |
该函数事实上就是对堆进行一个排序。
使用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)的更多相关文章
- STL algorithm算法merge(34)
merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...
- STL algorithm算法mismatch(37)
mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...
- STL algorithm算法is_permutation(27)
is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- STL algorithm算法minmax,minmax_element(36)
minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...
- 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& ...
- 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& ...
- STL algorithm算法mov,move_backward(38)
move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...
- STL algorithm算法lexicographical_compare(30)
lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...
随机推荐
- Android Support 包里到底有什么
大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 随着 ...
- UDP 打洞示例 包含 服务器 客户端
客户端示例: #include "Net.h" #include "../p2pInfo.h" int main() { CUdp udp; if (0!=u ...
- tab标签页(选项卡)插件
<body style="margin: 50px;"> <ul id="nav" class="nav nav-tabs" ...
- 微信浏览器的html5页面显示配置等问题汇集 1,禁止微信浏览器分享页面链接 (定点更新)
公司新需求,要求从安卓IOS应用分享到微信的链接页面不允许二次转发,所以开始编程第一步,上网找资料... 网上大神就是多,不消两分钟,找到方案,很简单,上代码先: wx.config({ debug: ...
- java文件处理 之 读写TXT(比之c++,重置文件头,int转string)
一:c/c++ 处理文件的使用方法.详见博客 c++文件操作 二:java与c++的方便之处: (1) java在读取文件时.能够对字符流进行处理,又一次进行编码,如 InputStreamReade ...
- 纯C++实现的HTTP请求封装(POST/GET)
纯C++实现的HTTP请求(POST/GET),支持windows和linux, 进行简单的封装, 方便调用.实现如下: #include "HttpConnect.h" #ifd ...
- 早该知道的 7 个JavaScript 技巧[转]
简洁写法 对象的简写在过去,如果你想创建一个对象,你需要这样: var car = new Object(); car.colour = 'red'; car.wheels = 4; car.h ...
- metabase实施文档
安装提前:需要安装JDK1.8以上 软件下载地址: https://metabase.com 还需要下载 ojdbc7.jar,以支持Oracle驱动 下载地址:http://www.oracle.c ...
- 【CS Round #43 A】Expected Dice
[链接]https://csacademy.com/contest/round-43/task/expected-dice/ [题意] 大水题 [题解] 把36种可能的结果都存下来. 然后把重复出现的 ...
- 从软件project的角度写机器学习3——主要监督学习算法的project性分析
主要机器学习算法的project适用性分析 前段时间AlphaGo跟李世石的大战及相关的深度学习的新闻刷了一遍又一遍的朋友圈.只是这件事情,也仅仅是在机器学习的深度上进一步拓展,而机器学习的广度(也即 ...