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 ...
随机推荐
- JDBC连接池C3P0
连接池 1)传统方式找DriverManager要连接.数目是有限的. 2)传统方式的close().并没有将Connection重用.仅仅是切断应用程序和数据库的桥梁,即无发送到SQL命令到数据库端 ...
- hdoj-1289-A Bug's Life【种类并查集】
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- DIV+CSS两种盒子模型(W3C盒子与IE盒子)
在辨析两种盒子模型之前.先简单说明一下什么叫盒子模型. 原理: 先说说我们在网页设计中常听的属性名:内容(content).填充(padding).边框(border).边界(margin), CSS ...
- 从 QSplitter 中移除 QWidget(使用隐藏与显示,切换十分方便,不要真正销毁)
Splitter 的函数中有addWidget,但是却没有removeWidget, 或者delete之类的功能,所以如果想删去或者暂时不显示其中的某些widget就要自己手动完成这个效果.方法一:取 ...
- js23---工厂模式1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- jQuery自定义插件规范
<ul class="list"> <li>导航列表 <ul class="nav"> <li>导航列表1< ...
- (转)Linux下查看Nginx Apache MySQL的并发连接数和连接状态
转自: http://www.ha97.com/4106.html 1.查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:netstat -n | awk '/^tcp/ { ...
- 洛谷 P1916 小书童——蚂蚁大战
P1916 小书童——蚂蚁大战 题目背景 小A在你的帮助下,开始“刷题”,他在小书童里发现了一款叫“蚂蚁大战”(又称蛋糕保卫战)的游戏.(你懂得) 题目描述 游戏中会出现n只蚂蚁,分别有a1,a2…… ...
- cv2.putText 文字换行('\n')无法解析换行
OpenCV putText() new line character cv2.putText 在向图像中添加文本信息时,如果在待添加的文本中含有换行转义符,一般它是无法正确处理的: cv2.putT ...
- IOS的UIWebView中JS点击事件,需要加入cursor:pointer;属性才可以
IOS的UIWebView中JS点击事件,需要加入cursor:pointer;属性才可以. Android的WebView可以支持外链样式,js文件:IOS则需要改为内嵌样式和JS文件.