priority_queue对任意数据类型进行排序比较

使用仿函数进行比较

#include
#include
using namespace std;
struct Node
{
	int a, b;
	bool operator<(Node& other)
	{
		return b < other.b;
	}
};
class cmp
{
public:
	bool operator()(Node& n1, Node& n2) const
	{
		return n1.b < n2.b;
	}
};
int main()
{
	priority_queue<Node, vector<Node>, cmp> q;
	q.push({ 1,2 });
	q.push({ 4,5 });
	q.push({ 3,4 });

	while (q.size())
	{
		auto e = q.top();
		q.pop();
		printf("%d %d\n", e.a, e.b);
	}

	return 0;
}

函数后加const

函数后加const表示这个函数中的参数是不能被改变的
const对象只能调用const函数

你可能感兴趣的:(算法)