I. RELATED definitions
The priority queue container, like a queue, can only insert elements from the end of a team and delete elements from the first team. But it has a feature that the largest element in the queue is always at the head of the team, so when the team is out, it does not follow the FIFO principle, but the largest element in the current queue. This is similar to the order in which the elements in the queue are sorted from large to small. The comparison rules for elements by default are sorted by element values from large to small, and the < operator can be overloaded to redefine the comparison rule.
The priority queue can be implemented with vectors (vector) or two-way queues (deque) (Note that list container cannot be used to implement a queue because the list iterator is not an arbitrary access iterator. And in the pop used in the heap sorting is required randomaccess iterator!) :
Priority_queue<vector<int>, less<int> > pq1; Sorting using incrementing less<int> function objects
Priority_queue<deque<int>, greater<int> > pq2; Sorting by using the Decrement greater<int> function object
Its member functions are "empty", "size", "Top", "Push", "stack (POP)", and so on.
Second, priority_queue
Basic operation:
Empty () returns true if the queue is null
Pop () Delete the top element, delete the first element
Push () joins an element
Size () returns the number of elements owned in the priority queue
Top () returns the priority queue to the top element, returning the element with the highest priority in the priority queue
In the default priority queue, high priority first-out team. In the default int type, the first team is the larger number.
Header file:
#include <queue>
How to declare:
1. Common method:
priority_queue<int> Q; By manipulating the elements from the large to the small order of the teamPriority_queue<int,vector<int>, greater<int> > Q; By operation, follow the elements from small to large order out of the team
2. Custom Priority:
struct CMP {operator BOOL () (int x, int y){return x > y; X small priority high//can also be written in other ways, such as: Return p[x] > P[y]; p[i] small priority high}};Priority_queue<int, Vector<int>, cmp> Q; Defining Methods//Where the second parameter is a container type. The third parameter is a comparison function.
3, the structure of the Declaration method:
struct Node {int x, y; friend bool Operator < (Node A, Node B){return a.x > b.x; Structure, the X-Small priority is high}};priority_queue<node>q; Defining Methods//In the structure, Y is the value and X is the priority. //By customizing the operator< operator to compare the precedence in the element. //When overloading "<", it is best not to Overload ">", a compile error may occurThird, the code implementation
Priority queue, its construction and implementation we can not go into the first, we now just need to understand its characteristics, and in the use of the problem.
Explain it as an example (well, after you've written it, you'll find that this code package is almost all we need to use, take a closer look):
/* Basic usage of priority queue 2017/8/1 xzxl*/#include <stdio.h> #include <functional> #include <queue> #include < ;vector> using namespace std; Define structure, use operator overloading, custom Priority 1 struct cmp1{bool operator () (int &a,int &b) {return a>b;//min precedence} }; struct cmp2{bool operator () (int &a,int &b) {return a<b;//maximum precedence}}; Define structure, use operator overloading, custom priority 2 struct number1{int x; BOOL operator < (const number1 &a) const {return x>a.x;//min precedence}}; struct number2{int x; BOOL operator < (const number2 &a) const {return x<a.x;//Max precedence}}; int a[]={14,10,56,7,83,22,36,91,3,47,72,0}; Number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0}; Number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0}; int main () {priority_queue<int>que;//constructs the queue with the default precedence priority_queue<int,vector<int>,cmp1>que1;//most Small Value Priority priority_queue<int,vector<int>,cmp2>que2;//Maximum priority priority_queue<int,vector<int>,greater<int> >que3;//Note that ">>" will be considered an error, This is the right-shift operator, so here's a space number to separate priority_queue<int,vector<int>,less<int> & gt;que4;////Maximum Priority priority_queue<number1>que5; priority_queue<number2>que6; int i; for (i=0;a[i];i++) {Que.push (a[i]); Que1.push (A[i]); Que2.push (A[i]); Que3.push (A[i]); Que4.push (A[i]); } for (i=0;num1[i].x;i++) Que5.push (Num1[i]); for (i=0;num2[i].x;i++) Que6.push (Num2[i]); printf ("with default precedence: \ n (priority_queue<int>que;) \ n"); printf ("Queue 0:\n"); while (!que.empty ()) {printf ("%3d", Que.top ()); Que.pop (); } puts (""); Puts (""); printf ("Adopt struct-Custom priority mode one: \ n (priority_queue<int,vector<int>,cmp>que;) \ n"); printf ("Queue 1:\n"); while (!que1.empty ()) {printF ("%3d", Que1.top ()); Que1.pop (); } puts (""); printf ("Queue 2:\n"); while (!que2.empty ()) {printf ("%3d", Que2.top ()); Que2.pop (); } puts (""); Puts (""); printf ("Use header file \" Functional\ "to define priority: \ n (priority_queue<int,vector<int>,greater<int>/less<int > >que;) \ n "); printf ("Queue 3:\n"); while (!que3.empty ()) {printf ("%3d", Que3.top ()); Que3.pop (); } puts (""); printf ("Queue 4:\n"); while (!que4.empty ()) {printf ("%3d", Que4.top ()); Que4.pop (); } puts (""); Puts (""); printf ("Adopt struct Custom Priority Method II: \ n (priority_queue<number>que) \ n"); printf ("Queue 5:\n"); while (!que5.empty ()) {printf ("%3d", Que5.top ()); Que5.pop (); } puts (""); printf ("Queue 6:\n"); while (!que6.empty ()) {printf ("%3d", Que6.top ()); Que6.pop (); } puts (""); return 0; }/* Run Result: Default precedence relationship: (Priority_queue<int>que;) Queue 0:83 72 56 47 36 22 14 10 7 3 Adopt structure Custom Priority Method one: (priority_queue<int,vector<int>,cmp>que;) Queue 1:7 Ten------------72, 56 47 36 22 14 10 7 3 The priority is defined in the header file "functional": (priority_queue< ;int,vector<int>,greater<int>/less<int> >que;) Queue 3:7 Ten-in-a-22---------------4:83 72 56 47 36, 14, 10 7 3 Adopt structure custom Priority Mode two: (PRIORITY_QUEUE<NUMBER&G T;que) Queue 5:7 72 56 47 36 22 14 10 7 3 * *
c++stl--Priority Queue