优先队列——priority_queue

priority_queue:优先队列,自动排序,默认为优先级较高的在前,但是要注意的是,它对int型变量的默认排序是从大到小,因为它认为值越大,优先级越高,与sort排序刚好相反。
1.对int型变量
priority_queue<int, vector < int > ,less < int > >从大到小排列
priority_queue<int , vector < int >,greater< int> >从小到大排列

2.对结构体排序,要重载一下 " < ",只能重载小于号

从小到大排列

struct node
{
	int x,y;
};
bool operator <( const node & a, const node & b)//重载运算符
{
	if(a.x!=b.x) return a.x>b.x;
	else
	return a.y>b.y;
}
priority_queue <node > que ;

从大到小排列

struct node
{
	int x,y;
};
bool operator <( const node & a, const node & b)//重载运算符
{
	if(a.x!=b.x) return a.x<b.x;
	else
	return a.y<b.y;
}
priority_queue <node > que ;
发布了32 篇原创文章 · 获赞 14 · 访问量 1498

猜你喜欢

转载自blog.csdn.net/weixin_43310882/article/details/104023716
今日推荐