priority_queue 优先队列

c++优先队列(priority_queue)用法详解

//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

关于优先队列中结构体的使用:

  参考:在优先队列中使用结构体的若干小结

struct node{
	int u,v;
	int w;//边的权 
};
bool operator < (const node& a,const node& b)
{//反过来,这样正好优先队列递增 
	return a.w>b.w;
}
/*定义比较操作符,这样node在priority_queue中自动排序*/

  

原文地址:https://www.cnblogs.com/liuyongliu/p/10320422.html