empty() 如果隊列爲空,則返回真
pop() 刪除對頂元素,刪除第一個元素
push() 加入一個元素
size() 返回優先隊列中擁有的元素個數
top() 返回優先隊列對頂元素,返回優先隊列中有最高優先級的元素
在默認的優先隊列中,優先級高的先出隊。在默認的int型中先出隊的爲較大的數。
頭文件:
#include <queue>
聲明方式:
1、普通方法:
priority_queue<int> q; //通過操作,按照元素從大到小的順序出隊
priority_queue<int,vector<int>, greater<int> > q; //通過操作,按照元素從小到大的順序出隊
2、自定義優先級:
struct cmp {
operator bool ()(int x, int y)
{
return x > y; // x小的優先級高 //也可以寫成其他方式,如: return p[x] > p[y];表示p[i]小的優先級高
}
};
priority_queue<int, vector<int>, cmp> q; //定義方法
//其中,第二個參數爲容器類型。第三個參數爲比較函數。
3、結構體聲明方式:
struct node {
int x, y;
friend bool operator < (node a, node b)
{
return a.x > b.x; //結構體中,x小的優先級高
}
};
priority_queue<node>q; //定義方法
//在該結構中,y爲值, x爲優先級。
//通過自定義operator<操作符來比較元素中的優先級。
//在重載”<”時,最好不要重載”>”,可能會發生編譯錯誤