STL Priority Queue Detailed

Source: Internet
Author: User

Priority QueueThe priority queue is an abstract data type (abstract Date Type,adt) that behaves like a queue, but the first-out team element is not an element of the advanced queue, but the highest-priority element in the queue.
The priority queue of STL is defined in header file <queue> (same as queue), declared with "PRIORITY_QUEUE<INT>PQ";
The most basic usage

Definition: PRIORITY_QUEUE<INT>PQ;

operation:

Pq.empty () If the queue is empty returns True

Pq.pop () Remove the top element

Pq.push () add an element

pq.size () returns the number of elements owned in the priority queue

Pq.top () returns the priority queue to the top element

Here we describe how several priority queues are defined:

          PRIORITY_QUEUE&LT;INT&GT;PQ The default is that the larger the integer, the higher the priority, if you want to make him a smaller number of priority, what to do?
There are also templates in STL "Priority_queue<int,vector<int>,greater<int > &GT;PQ"
          Above the angle brackets within the firstParametersis a queued element type (int), the second is a container type (vector<int>), and the third is a comparison function (greater<int>)
          So we can also customize
priority_queue<int,vector<int>,cmp1 >pq; min priority
PRIORITY_QUEUE<INT,VECTOR<INT>,CMP2 >pq2; Maximum priority (some of the online wording is wrong, this is the author after testing the definition of the way)

CMP1,CMP2 is implemented in the structure body
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      }  };  


Of course, we can also set the priority of custom types
struct node  //Custom type priority setting {    friend bool operator< (node n1, node N2)//overloaded less than sign, do not overload greater than sign, may error    {        return N1.priority < n2.priority;//(priority) Big precedence high    } int priorities    ;    int value;};

Finally, the author's test code:
 #include <queue> #include <vector> #include <stdio.h>//#include <functional.h>using namespace  Std;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}}; struct node//custom type priority setting {friend bool operator< (node n1, node N2)//overloaded less than sign, do not overload greater than sign, may error {return N1.prior    ity < n2.priority;//(priority) big precedence High} int priorities; int value;}; int main () {int a[6]={1,5,2,8,6,3};p rintf ("max precedence template: \ n");p riority_queue<int>pq0;for (int i=0;i<6;i++) { Pq0.push (A[i]);} for (int i=0;i<6;i++) {printf ("%d", Pq0.top ());p q0.pop ();} printf ("\ n");p rintf ("Maximum precedence custom \ n");p riority_queue<int,vector<int>,cmp2 >pq2;for (int i=0;i<6;i++) { Pq2.push (A[i]);} for (int i=0;i<6;i++) {printf ("%d", Pq2.top ());p q2.pop ();} printf ("\ n");p rintf ("minimum precedence template: \ n");p riority_queue<int,vector<int>,greater<int> &GT;PQ3;//note here there are spaces between the two > > otherwise the compiler will error g++for (int i=0;i<6;i++) {Pq3.push (a[i]);} for (int i=0;i<6;i++) {printf ("%d", Pq3.top ());p q3.pop ();} printf ("\ n");p riority_queue<int,vector<int>,cmp1 >pq;printf ("min-precedence-custom: \ n"), for (int i=0;i<6;i++) { Pq.push (A[i]);} for (int i=0;i<6;i++) {printf ("%d", Pq.top ());p q.pop ();} printf ("\ n");p rintf ("Custom type priority: \ n");p riority_queue<node> pq4;node b[5];b[0].priority = 6;         B[0].value = 1; B[1].priority = 9;         B[1].value = 5; B[2].priority = 2;         B[2].value = 3; B[3].priority = 8;         B[3].value = 2; b[4].priority = 1; B[4].value = 4; for (int i=0;i<5;i++) {Pq4.push (b[i]);}    printf ("Priority value \ n"), for (int i=0;i<5;i++) {printf ("%d\t%d\n", Pq4.top (). Priority,pq4.top (). Value);p q4.pop ();} return 0;}
(If there is an error, please correct it, reproduced in the source)





STL Priority Queue Detailed

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.