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 ...
随机推荐
- 有關AWS EC2 (EBS 收費)的問題
有關AWS EC2 (EBS 收費)的問題 之前一陣子的時候,公司在使用Amazone Web Service (AWS)的 EC2 (Amazon Elastic Compute Cloud).不過 ...
- alert警告框
标签中写: <div class="alert alert-warning fade in"> <button class="close" d ...
- InstallShield详细制作说明(二)
四.设置安装的组件Component
- Linux库文件路径的添加
库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的.一般 Linux 系统把 /lib 和 /usr/lib 两个目录作为默认的库搜索路径,所以使用 ...
- CF1009F Dominant Indices(树上DSU/长链剖分)
题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...
- Android学习之图片压缩,压缩程度高且失真度小
曾经在做手机上传图片的时候.直接获取相机拍摄的原图上传,原图大小一般1~2M.因此上传一张都比較浪费资源,有些场景还须要图片多张上传,所以近期查看了好多前辈写的关于图片处理的资料.然后试着改了一个图片 ...
- android对话框(Dialog)的使用方法
Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- 104.tcp多线程读写实现群聊
客户端: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <w ...
- 1.2 Use Cases中 Website Activity Tracking官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Website Activity Tracking 网站活动追踪 The origi ...
- Python 极简教程(七)列表 list
由于列表过于重要,请认真看完并保证所有代码都敲过一遍. 什么是列表 列表是 Python 中最常用的数据结构,也是一种数据类型,其样式如下: li = [1, 2, 3, 'a', 'b'] 列表是一 ...