C ++ | priority_queue usage (including custom sort)

priority_queue is essentially a heap.

1. The file header is #include <queue>

2. Comparing the elements of priority_queue

  Affirmed template with three parameters: priority_queue <Type, Container, Functional>, where Type is the type of data, Container storage container for the data, Functional element of the comparative embodiment.

  Container must be implemented with an array of containers, such as vector, deque and the like, but can not list. STL which is used by default vector.

2.1 Comparison with the default mode operator <, so if the default parameters of the back 2, then the priority queue is large stack top (descending), the maximum head elements. Pay special attention to the comparison function pair .

  The following code returns a descending output:

 
 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 int main(){
 5     priority_queue<int> q;
 6     for( int i= 0; i< 10; ++i ) q.push(i);
 7     while( !q.empty() ){
 8         cout<<q.top()<<endl;
 9         q.pop();
10     }
11     return 0;
12 }
 

Substituting the following pair of code returns a comparison result, according to the first pair First element descending, first elements are equal, then in accordance with the second element in descending order:

 
 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 int main(){
 6     priority_queue<pair<int,int> > coll;
 7     pair<int,int> a(3,4);
 8     pair<int,int> b(3,5);
 9     pair<int,int> c(4,3);
10     coll.push(c);
11     coll.push(b);
12     coll.push(a);
13     while(!coll.empty())
14     {
15         cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
16         coll.pop();
17     }
18     return 0;
19 }
 

2.2 If we want to top the small pile, the general template should bring in all three parameters. STL is defined inside a functor greater <>, the basic types can be declared functor small stack top .

  The following code returns an ascending output:

 
 1 #include <iostream>
 2 #include <queue> 
 3 using namespace std;
 4 int main(){
 5     priority_queue<int, vector<int>, greater<int> > q;
 6     for( int i= 0; i< 10; ++i ) q.push(10-i);
 7     while( !q.empty() ){
 8         cout << q.top() << endl;
 9         q.pop();
10     }
11     return 0;
12 }
 

Substituting the following pair of code returns a comparison result, according to the first pair First ascending elements, first element equal, then in accordance with the second element in ascending order: 

 
 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 int main(){
 6     priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > coll;
 7     pair<int,int> a(3,4);
 8     pair<int,int> b(3,5);
 9     pair<int,int> c(4,3);
10     coll.push(c);
11     coll.push(b);
12     coll.push(a);
13     while(!coll.empty())
14     {
15         cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
16         coll.pop();
17     }
18     return 0;
19 }
 

2.3 For custom type, must override operator <or overwrite functor.

2.3.1 Overload operator <Examples: returns true, the parameter described in the left lower priority than the right parameter

 
#Include. 1 <the iostream> 
 2 #include <Queue> 
 . 3 the using namespace STD; 
 . 4 the Node {struct 
 . 5 int X, Y; 
 . 6 the Node (int A = 0, B = 0 int): 
 . 7 X (A), Y (B ) {} 
 . 8}; 
 . 9 BOOL operator <(a Node, Node B) {return true //, described a lower priority than B 
10 X // Node large value lower priority (x row of small Node before team) 
equal 11 // x, y large low priority (small y teams ranked in the top Node) 
12 is IF (== AX BX) return AY> by; 
13 is return AX> BX; 
14} 
15 int main () { 
16 The priority_queue <the Node> Q; 
. 17 for (int I = 0; I <10; I ++) 
18 is q.push (the Node (RAND (), RAND ())); 
!. 19 the while (Q. empty ()) { 
. 20 is COUT << q.top () << X '' << q.top () << endl Y.;
21         q.pop();
22     }
23     return 0;
24 }
 

After a custom type overloaded operator <, when you can just declare an object with a template parameter .

But this time not as basic types declare priority_queue <Node, vector <Node>, greater <Node>>, because the greater <Node> is not defined, if you want to define in this way can override operator>.

Examples: Returns the top of a small stack. But do not use, habits are overloaded operator <.

 
#Include. 1 <the iostream> 
 2 #include <Queue> 
 . 3 the using namespace STD; 
 . 4 the Node {struct 
 . 5 int X, Y; 
 . 6 the Node (int A = 0, B = 0 int): 
 . 7 X (A), Y (B ) {} 
 . 8}; 
 . 9 BOOL operator> (the Node a, the Node B) {// returns true, a priority higher than B 
10 X // large portion of the front row in the team; the same X, y rows in a large the front portion team 
. 11 IF (== AX BX) return AY> by; 
12 is return AX> BX; 
13 is} 
14 int main () { 
15 The priority_queue <the Node, Vector <the Node>, Greater <the Node>> Q; 
16 for ( 0 = I int; I <10; I ++) 
. 17 q.push (the Node (RAND (), RAND ())); 
(! q.empty () 18 is the while) { 
. 19 << q.top COUT () . .x << '' << q.top ( ) y << endl;
20         q.pop();
21     }
22     return 0;
23 }
 

2.3.2 Examples override functor (return value is the same sort 2.3.1, are small stack top press ascending x, x are equal, then ascending y.):

 
#Include. 1 <the iostream> 
 2 #include <Queue> 
 . 3 the using namespace STD; 
 . 4 the Node {struct 
 . 5 int X, Y; 
 . 6 the Node (int A = 0, B = 0 int): 
 . 7 X (A), Y (B ) {} 
 . 8}; 
 . 9 CMP struct { 
10 BOOL operator () (the Node a, the Node b) {// the default function is less 
return true 11 //, a lower priority than the priority of b (a row follows b) 
12 is IF (== AX BX) return AY> by;       
13 is return AX> BX;} 
14}; 
15 int main () { 
16 the priority_queue <the Node, Vector <the Node>, CMP> Q; 
. 17 for ( 0 = I int; I <10; I ++) 
18 is q.push (the Node (RAND (), RAND ())); 
. 19 the while (q.empty (!)) {  
20 is COUT << q.top () .x << '' << q.top().y << endl;
21 is q.pop (); 
22 is}
23     return 0;
24 }

 

Guess you like

Origin www.cnblogs.com/shona/p/12163381.html