STL中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 );

默认的使用operator< 进行比较。而我们可以自定义comp进行比较,来进行建堆。

其中,两个make_heap所使用的参数,[first,last)     这个区间是半开半闭的。

当我们需要对堆进行存取操作时,我们有函数,pos_heap,push_heap

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

使用pop_heap 操作后, 最大值被移动到last-1的位置。[first ,last-1) 之间的元素继续保持堆的形态。

我们只需取出最后一个元素即可。也就是vec.pop_back();

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

执行push_heap 时, [first,last-1)个元素是保持堆形态的,如果不是堆,则会报错。

这个函数是相当于对堆进行调整。

Code:


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
vector heap;
int n;
int main()
{
cout<<"Insert numbers of heap:";
cin>>n;
cout<<"Insert values of each members:\n";
for(int i=0;i<n;i++)
{
int read;
cin>>read;
heap.push_back(read);
}
make_heap(heap.begin(),heap.end());//大根堆
//make_heap(heap.begin(),heap.end(),greater());小根堆
cout<<"If there's something trouble,insert \"help\"!\n";
cout<<"Heap has neen maken!\nPlease insert commands:\n";
while(1)
{
string s;
cin>>s;
if(s=="pop")
{
pop_heap(heap.begin(),heap.end());
cout<<heap[heap.size()-1];
heap.pop_back();
cout<<endl;
}
else if(s=="push")
{
int read;
cin>>read;
heap.push_back(read);
push_heap(heap.begin(),heap.end());
}
else if(s=="top")
{
cout<<heap.front()<<endl;
}
else if(s=="sort")//奇怪的是,这里大根堆的堆排序是由小到大的(就是把数列逆转了)
{
vector temp;
temp=heap;
sort_heap(temp.begin(),temp.end());
for(int i=0;i<temp.size();i++)
cout<<temp[i]<<' ';
cout<<endl;
}
else if(s=="out")
{
for(int i=0;i<heap.size();i++)
cout<<heap[i]<<' ';
cout<<endl;
}
else if(s=="end")
{
return 0;
}
else if(s=="help")
{
cout<<"1.Pop the top number of heap,please insert \"pop\"!\n";
cout<<"2.Push a number into heap,please insert \"push numbers\"!\n";
cout<<"3.Look the top number of heap,please insert \"top\"!\n";
cout<<"4.Sort the heap and import result,please insert \"sort\"!\n";
cout<<"5.Look the array of heap,please insert \"out\"!\n";
cout<<"6.End the program,please insert \"end\"!\n";
}
else
cout<<"Bad Commands!"<<endl;
}
return 0;
}

算法库中heap应用的更多相关文章

  1. C++神奇算法库——#include<algorithm>

    算法(Algorithm)为一个计算的具体步骤,常用于计算.数据处理和自动推理.C++ 算法库(Algorithms library)为 C++ 程序提供了大量可以用来对容器及其它序列进行算法操作的函 ...

  2. mahout算法库(四)

    mahout算法库 分为三大块 1.聚类算法 2.协同过滤算法(一般用于推荐) 协同过滤算法也可以称为推荐算法!!! 3.分类算法 算法类 算法名 中文名 分类算法               Log ...

  3. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  4. 8、泛型程序设计与c++标准模板库4.标准c++库中的算法

    标准c++算法是通过迭代器和模板来实现的,其实算法本身就是一种函数模板. 算法从迭代器那里获得一个元素,而迭代器则知道一个元素在容器中的什么位置.迭代器查找元素的位置并将这些信息提供给算法以便算法能够 ...

  5. scikitlearn库中调用k-近邻算法的操作步骤

    1.k近邻算法可以说是唯一一个没有训练过程的机器学习算法,它含有训练基础数据集,但是是一种没有模型的算法,为了将其和其他算法进行统一,我们把它的训练数据集当做它的模型本身.2.在scikitlearn ...

  6. 安装Python算法库

    安装Python算法库 主要包括用NumPy和SciPy来处理数据,用Matplotlib来实现数据可视化.为了适应处理大规模数据的需求,python在此基础上开发了Scikit-Learn机器学习算 ...

  7. scikit-learn 支持向量机算法库使用小结

    之前通过一个系列对支持向量机(以下简称SVM)算法的原理做了一个总结,本文从实践的角度对scikit-learn SVM算法库的使用做一个小结.scikit-learn SVM算法库封装了libsvm ...

  8. scikit-learn 线性回归算法库小结

    scikit-learn对于线性回归提供了比较多的类库,这些类库都可以用来做线性回归分析,本文就对这些类库的使用做一个总结,重点讲述这些线性回归算法库的不同和各自的使用场景. 线性回归的目的是要得到输 ...

  9. 算法库:Matlab与C++混合编程

    算法库:Matlab与C++混合编程 最近做光流算法预演过程中,下载的源码中涉及到了Matlab和C++的混合编程.在同事Matlab2014的环境下,程序到是一下就运行通过了.但在我这Matlab2 ...

随机推荐

  1. Java数组的应用2:数组的最大,最小,求和,平均值,倒置

    import java.util.Scanner; public class HelloWorld { public static void main(String[] args){ // Scann ...

  2. (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)

    上篇说道要想将手臂固定在机器人身体上,而且手臂还能转动,简单的办法是使用物理关节.但这不是只有这种办法.用关节固定物体有时候不能满足需要,这时必须自己动手写代码处理,后面会介绍另一种固定的方法. 在S ...

  3. python“# -*- coding: UTF-8 -*-”

    python跑一趟红 python脚本文件中,python编译器是使用ascii码来解释脚本内容.如果.py源文件中包含中文,会报错(注释也报错).所以文件开头加上"# -*- coding ...

  4. R12 - Error 'Unable to process your transaction. The operating unit is either invalid or it cannot b

    In this Document   Symptoms   Cause   Solution   Still Have Questions?   References APPLIES TO: Orac ...

  5. 分布式进阶(十六)Zookeeper入门基础

    Zookeeper入门基础 前言 在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据.如果在创建znode时Flag设置为EPHEMERAL,那么当 ...

  6. 使用HTML5抓取 Audio & Video

    原文地址: http://www.html5rocks.com/en/tutorials/getusermedia/intro/ 本地化的文章: http://www.html5rocks.com/z ...

  7. linux 网络不通问题排查

    基本的排错步骤(从上往下)ping 127.0.0.1ping的通说明tcp协议栈没有问题ping 主机地址 ping的通说明网卡没有问题ping 路由器默认网关 ping的通说明包可以到达路由器最后 ...

  8. Android 高逼格纯代码实现类似微信钱包带分割线的GridView

    前言    原文地址:http://blog.csdn.net/sk719887916/article/details/40348837: Tamic 通过上两篇关于自定view的文章,在自定义vie ...

  9. 【LaTeX排版】LaTeX论文排版<四>

    1.表格的插入     一般的表格插入的代码如下: \begin{table}[H] \centering \begin{tabular}{|c|c|c|} \hline 感知方法&优点&am ...

  10. Linux - quota的举例说明

    实作 Quota 流程-1:文件系统支持 [root@www ~]# df -h /home Filesystem Size Used Avail Use% Mounted on /dev/hda3 ...