If it's a queue, first include the header file.#include <queue>
The priority queue has all the attributes of the queue, including the basic operations, but adds an internal sort on that basis, which is essentially a heap implementation.
Definition: priority_queue<Type, Container, Functional>
Type is the data type,Container is the container type (Container must be an array implementation of the container, such as vector,deque, etc., but not the list. STL is used by default vector,functional is the way to compare, when you need to use a custom data type to pass these three parameters, when using the basic data type, only need to pass the data type, the default is the Big Top heap.
// Ascending queue priority_queue <int,vector<int>,greater<int> > q; // Descending queue priority_queue <int,vector<int>,less<int> >q; // greater and less are the two functor implementations of STD (that is, the use of a class looks like a function.) Its implementation is to implement a operator () in the class, and this class has a function similar to the behavior of a pseudo function class.
1. Basic type examples:
#include <iostream>#include<queue>using namespacestd;intMain () {//the default for the underlying type is the large top heappriority_queue<int>A; //equivalent to Priority_queue<int, Vector<int>, less<int> > A; //there must be a space here, or a right-shift operator ↓priority_queue<int, vector<int, greater<int> > C;//This is the small top pile .priority_queue<string>b; for(inti =0; I <5; i++) {A.push (i); C.push (i); } while(!A.empty ()) {cout<< a.top () <<' '; A.pop (); } cout<<Endl; while(!C.empty ()) {cout<< c.top () <<' '; C.pop (); } cout<<Endl; B.push ("ABC"); B.push ("ABCD"); B.push ("CBD"); while(!B.empty ()) {cout<< b.top () <<' '; B.pop (); } cout<<Endl; return 0;}
Output:
4 3 2 1 0 0 1 2 3 4 CBD ABCD ABC
A comparison of 2.pair, first comparing the first element, the first equality comparing the second
#include <iostream>#include<queue>#include<vector>using namespacestd;intMain () {priority_queue<pair<int,int> >A; Pair<int,int> B (1,2); Pair<int,int> C (1,3); Pair<int,int> D (2,5); A.push (d); A.push (c); A.push (b); while(!A.empty ()) {cout<< A.top (). First <<' '<< A.top (). Second <<'\ n'; A.pop (); }}
Output:
2 5 1 3 1 2
3. For a custom type
#include <iostream>#include<queue>using namespacestd;//Method 1structTmp1//operator Overloading <{ intx; TMP1 (inta) {x =A;} BOOL operator< (Consttmp1& a)Const { returnx < a.x;//Big Top Pile }};//Method 2structTmp2//overriding an affine function{ BOOL operator() (Tmp1 A, tmp1 b)//tmp1 as template data type {returna.x < b.x;//Big Top Pile }};intMain () {Tmp1 A (1); TMP1 B (2); TMP1 C (3); Priority_queue<tmp1>D; D.push (b); D.push (c); D.push (a); while(!D.empty ()) {cout<< d.top (). x <<'\ n'; D.pop (); } cout<<Endl; Priority_queue<TMP1, Vector<tmp1>, tmp2>F; F.push (c); F.push (b); F.push (a); while(!F.empty ()) {cout<< f.top (). x <<'\ n'; F.pop (); }}
Output:
3 2 1 3 2 1
It is generally defined in the form of an overriding functor.
Use of Priority queues in C + +