The use of Priority_queue

Source: Internet
Author: User

Priority_queue essence is a heap.

1. header file is #include<queue>

2. Comparison of elements in Priority_queue

Template declaration with 3 parameters: Priority_queue<type, Container, Functional>, where type is the data type, Container is the container that holds the data, and functional is the element comparison method.

Container must be an array-implemented container, such as Vector,deque, and so on, but not a list. In STL, vectors are used by default.

2.1 Comparison mode by default with OPERATOR<, so if the following 2 parameters default, the priority queue is the Big Top heap (descending), the largest team head element.

The following code returns a descending output:

1#include <iostream>2#include <queue>3 using namespacestd;4 intMain () {5priority_queue<int>Q;6      for(intI=0; i<Ten; ++i) Q.push (i);7      while( !Q.empty ()) {8Cout<<q.top () <<Endl;9 Q.pop ();Ten     } One     return 0; A}

  

2.2 If you want to use a small top heap, you generally need to take 3 parameters of the template in. The STL defines a functor Greater<>, and the base type can declare a small top heap with this functor .

The following code returns an ascending output:

1#include <iostream>2#include <queue>3 using namespacestd;4 intMain () {5priority_queue<int, vector<int, greater<int> >Q;6      for(intI=0; i<Ten; ++i) Q.push (Ten-i);7      while( !Q.empty ()) {8cout << q.top () <<Endl;9 Q.pop ();Ten     } One     return 0; A}

2.3 For custom types, you must overload operator< or override the functor.

Example of 2.3.1 overloaded operator<: Returns True when the left parameter has a lower priority than the right parameter

1#include <iostream>2#include <queue>3 using namespacestd;4 structnode{5     intx, y;6Node (intA=0,intb=0):7 x (a), Y (b) {}8 };9 BOOL operator< (Node A, Node B) {//returns True, stating that A has a priority lower than BTen     //a node with a large X-value has a low priority (x small node in front of the team) One     //when x is equal, Y has a low priority (Y-small node is in front of the team) A     if(a.x== b.x)returnA.y>b.y; -     returnA.x>b.x; - } the intMain () { -Priority_queue<node>Q; -      for(intI=0; i<Ten; ++i) - Q.push (Node (rand (), rand () )); +      while( !Q.empty ()) { -cout << q.top (). x <<' '<< q.top (). Y <<Endl; + Q.pop (); A     } at     return 0; -}

after the custom type overloads the operator<, the object can be declared with only one template parameter .

But this cannot be declared priority_queue<node,vector<node>,greater<node> like the basic type, because greater<node> is not defined, If you want to define this method, you can overload operator >.

Example: A small top heap is returned. But not how to use, habit is overloaded operator<.

1#include <iostream>2#include <queue>3 using namespacestd;4 structnode{5     intx, y;6Node (intA=0,intb=0 ):7 x (a), Y (b) {}8 };9 BOOL operator> (Node A, Node B) {//return True,a priority is greater than BTen     //x is in the front of the team, and X is at the front of the team. One     if(a.x== b.x)returnA.y>b.y; A     returnA.x>b.x; - } - intMain () { thepriority_queue<node,vector<node>,greater<node> >Q; -      for(intI=0; i<Ten; ++i) - Q.push (Node (rand (), rand () )); -      while( !Q.empty ()) { +cout << q.top (). x <<' '<< q.top (). Y <<Endl; - Q.pop (); +     } A     return 0; at}

2.3.2 Overrides an example of an affine function (the return value sort is the same as 2.3.1, both of which are small top heaps. First by x Ascending, x equal, then y Ascending):

1#include <iostream>2#include <queue>3 using namespacestd;4 structnode{5     intx, y;6Node (intA=0,intb=0 ):7 x (a), Y (b) {}8 };9 structcmp{Ten     BOOL operator() (Node A, Node B) {//The default is the less function One         //when True, the priority of a is lower than the priority of B (A is at the back of B) A         if(a.x== b.x)returnA.y>b.y;  -         returnA.x>b.x;} - }; the intMain () { -Priority_queue<node, Vector<node>, cmp>Q; -      for(intI=0; i<Ten; ++i) - Q.push (Node (rand (), rand () )); +      while( !Q.empty ()) { -cout << q.top (). x <<' '<< q.top (). Y <<Endl; + Q.pop (); A     } at     return 0; -}

Reference: http://www.cnblogs.com/flyoung2008/articles/2136485.html

The use of Priority_queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.