您的位置:首页 > 其它

最大堆最小堆(自定义类型)

2013-03-24 18:47 260 查看
#include<queue>
#include "stdio.h"
using namespace std;
struct node
{
	int x , y;
	bool operator <(node a)const //最大堆重载
	{
		return y<a.y;
	}
	bool operator >(node a)const//最小堆重载
	{
		return y>a.y;
	}
};
int main(int argc, char* argv[])
{
	priority_queue<node> max;
	priority_queue<node,vector<node>,greater<node> > min;
	int i ;
	for( i = 0 ; i<10 ; i++)
	{
		node n ;
		n.x = i ;
		n.y = i;
		max.push(n);
		min.push(n);
	}
	for( i = 0 ; i<10 ; i++)
	{
		printf("%d ", max.top().y);
		max.pop();
	}
	for( i = 0 ; i<10 ; i++)
	{
		printf("%d ", min.top().y);
		min.pop();
	}
	printf("Hello World!\n");

	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: