Priority queue --priority_queue

priority_queue: priority queue, automatic sorting, the default is higher priority first, but be aware that the default sort it int variable is descending, because it believes that the larger the value, the higher the priority, and sort ordering just the opposite.
1. int variables
priority_queue <int, vector <int> , less <int>> in descending order
priority_queue <int, vector <int> , greater <int>> in ascending order

2. The structure of the sort, to override it, "<", only less than overloading No.

Not Sorted

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 ;

Descending order

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 ;
Published 32 original articles · won praise 14 · views 1498

Guess you like

Origin blog.csdn.net/weixin_43310882/article/details/104023716