头文件
基本操作和普通队列一样
top 访问队头元素
empty 队列是否为空
size 返回队列内元素个数
push 插入元素到队尾 (并排序)
pop 弹出队头元素
swap 交换内容
定义方式
priority_queue<type,container,方式>
int main()
{
int n[6] = { 2,2,1,5,6,5,};
priority_queue<int, vector<int>, greater<int>> a;//大根堆从小到大排序,不去重
priority_queue<int, vector<int>, less<int>> b;//从大到小
for (int i = 0; i < 6; i++) a.push(n[i]);
for (int i = 0; i < 6; i++) b.push(n[i]);
while (!a.empty())
{
cout << a.top() << " ";
a.pop();
}
cout << endl;
while (!b.empty())
{
cout << b.top() << " ";
b.pop();
}
}
结构体的自定义比较
struct node
{
int x, y;
};
struct cmp1//自定义比较
{
bool operator()(const node &a, const node &b) const
{
return a.x < b.x;//若x小那么结构体就小
}
};
struct cmp2
{
bool operator()(const node &a, const node &