10
22
2015
0

优先队列priority_queue操作

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
struct cmp{
	bool operator()(const int &a,const int &b)
	{
		return a>b;
		//if(a<b) return false;等效
		//< 为降序排序 大根堆  
	}
};
int main()
{
	priority_queue<int> pque; 
	pque.push(0);
	pque.push(3);
	pque.push(5);
	pque.push(1);
	while(!pque.empty())
	{
		printf("%d ",pque.top());//默认从大到小 
		pque.pop();
	}  
	printf("\n");
	priority_queue<int,vector<int>,cmp> pq;//注意vector拼写 
//	priority_queue<int,vector<int>,greater<int> > pq; 等效 
	pq.push(0);
	pq.push(3);
	pq.push(5);
	pq.push(1);
	while(!pq.empty())
	{
		printf("%d ",pq.top());
		pq.pop();
	}
	return 0;
}
Category: STL | Tags: | Read Count: 664

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com