Original address
Speaking of the queue, we first think of is first-out, backward-out, then what is the priority queue, in the priority queue, the element is given priority, when the element is accessed, the element with the highest priority is first accessed. That is, the priority queue has the highest first-out behavior characteristics.
Priority queue in header file # include <queue>;
Its declared format is: Priority_queue <int> ans;//declares a prioritized queue of shaping called ans
The basic operations are:
Empty ()//Determine if a queue is null
Pop ()//delete team top element
Push ()//Add an element
Size ()//Returns the number of elements owned in the priority queue
Top ()//return top element of priority queue
The time complexity of the priority queue is O (Logn), n is the number of elements in the queue, and its access takes time.
In the default priority queue, the first-out team with the highest priority. The default int type of priority queue is first out of the queue for the larger number of queues.
However, more often than not, we want to be able to customize its priorities, and here are a few common definitions of priority actions:
#include <iostream>#include<vector>#include<queue>using namespacestd; inttmp[ -]; structCMP1 {BOOL operator()(intXinty) {returnx > y;//high priority for small } }; structCMP2 {BOOL operator()(Const intXConst inty) {returnTMP[X] >Tmp[y]; //tmp[] Small priority, because you can change the value of the team outside the team,//so using this method is not a real priority, it is recommended to use struct type. } }; structNode {intx, y; FriendBOOL operator<(Node A, Node B) {returna.x > b.x;//structure, the X-Small priority is high } }; Priority_queue<int>Q1; Priority_queue<int, vector<int, cmp1>Q2; Priority_queue<int, vector<int, cmp2>Q3; Priority_queue<node>Q4; intMain () {intI,j,k,m,n; intx, y; Node A; while(cin>>N) { for(intI=0; i<n;i++) {cin>>a.y>>a.x; Q4.push (a); } cout<<Endl; while(!Q4.empty ()) {cout<<q4.top (). Y <<" "<<q4.top () .x<<Endl; Q4.pop (); } cout<<Endl; } return 0; }
Go Use of priority queues in C++stl