关于STL中heap的用法

在STL中heap的用法主要是make_heap(),push_heap(),pop_heap()的用法。具体均在代码中:

// range heap example  用heap构造出来的实际上是一棵树

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
	int myints[] = {10,20,30,5,15};
	vector<int> v(myints,myints+5);
	vector<int>::iterator it;
	make_heap (v.begin(),v.end());//male_heap就是构造一棵树,使得每个父结点均大于等于其子女结点
	cout << "initial max heap   : " << v.front() << endl;
	/* */
	pop_heap (v.begin(),v.end());//pop_heap不是删除某个元素而是把第一个和最后一个元素对调后[first,end-1]进行构树,最后一个不进行构树
	v.pop_back();//删除最后一个的结点
	cout << "max heap after pop : " << v.front() << endl;
	v.push_back(99);//在最后增加一个结点
	push_heap (v.begin(),v.end());//重新构树
	cout << "max heap after push: " << v.front() << endl;
	sort_heap (v.begin(),v.end());//把树的结点的权值进行排序
								  /*for(int j=0;j<v.size();j++)
								  cout<<v[j]<<' ';
	cout<<endl;*/
	cout << "final sorted range :";
	for (unsigned i=0; i<v.size(); i++) 
		cout << " " << v[i];
	cout << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值