优先队列:
STL:Priority_queue(Type, Container, Functional)
Type:即类型,Container即包含数据的容器,默认vector<int>,Functional即定义元素比较方式。
与队列类似,特性就是加入元素后自行排序。
priority_queue包含函数:empty() 判断空与否,size() 求元素个数,top() 返回队首元素,push() 队尾入列,pop() 队首出列
参考博客:https://www.cnblogs.com/xzxl/p/7266404.html(包括了大多定义形式,但没有deque为容器的定义,deque在其中作用不好发挥,因此不谈)
-
#include<cstdio> #include<functional> //greater<int>,less<int>的库 #include<queue> #include<vector> using namespace std; struct node { int x,y; bool operator > (const node &a) const //less只能重载< ,greater只能重载 > { return x > a.x; //> 则是从小到大,< 则从大到小 } }tree[100],now; int main() { priority_queue<int>pq1; //默认less<int>从大到小 priority_queue<int, vector<int>, greater<int> >pq2; //从小到大 priority_queue<node, vector<node>, greater<node> >pq3; now.x = 1, now.y = 1; pq3.push(now); now.x = 2, now.y = 2; pq3.push(now); now.x = 3, now.y = 3; pq3.push(now); printf("%d\n",pq3.top()); return 0; }