priority_queue 优先队列,其底层是用堆来实现的。在优先队列中,队首元素一定是当前队列中优先级最高的那一个。
在优先队列中,没有 front() 函数与 back() 函数,而只能通过 top() 函数来访问队首元素(也可称为堆顶元素),也就是优
先级最高的元素。基本操作有:
empty() 如果队列为空返回真
pop() 删除对顶元素
push() 加入一个元素
size() 返回优先队列中拥有的元素个数
top() 返回优先队列对顶元素
#include <queue>
#include <iostream>
using namespace std;
void main()
{
priority_queue<int> p1;//默认情况下是最大值优先级队列,相当于priority_queue<int,vector<int>,less<int>>
priority_queue<int, vector<int>, greater<int>> p2;//最小值优先级队列
p1.push(11);
p1.push(33);
p1.push(55);//虽然是后进入队列,由于是最大值优先,所以此项为队头
p1.push(6);
cout << "p1的队头为:" << p1.top() << endl;
while (p1.size()>0)
{
cout << p1.top() << " ";
p1.pop();
}
cout << endl;
system("pause");
}
更多内容见:https://blog.csdn.net/hellokandy/article/details/81458663