c++stl--Priority Queue

Source: Internet
Author: User

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 operation, according to the elements from the big to the small order out of the team priority_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 high Priority}};p Riority_queue<int, Vector<int>, cmp> Q    ; Define the method//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; Define the method//in the structure, Y is the value and X is the priority. The priority in the element is compared by customizing the operator< operator. It is best not to overload ">" When Overloading "<", a compilation error may occur
/*basic use of priority queues 2017/8/1 XZXL*/#include<stdio.h>#include<functional>#include<queue>#include<vector>using namespacestd;//defining structures, using operator overloading, customizing Priority 1structcmp1{BOOL operator()(int&a,int&b) {         returna>b;//Minimum value First    } }; structcmp2{BOOL operator()(int&a,int&b) {         returna<b;//Maximum Value First    } }; //defining structures, using operator overloading, customizing priority 2structnumber1{intx; BOOL operator< (ConstNumber1 &a)Const {         returnx>a.x;//Minimum value First    } }; structnumber2{intx; BOOL operator< (ConstNumber2 &a)Const {         returnx<a.x;//Maximum Value First    } }; inta[]={ -,Ten, About,7, the, A, $, the,3, -, the,0}; Number1 num1[]={ -,Ten, About,7, the, A, $, the,3, -, the,0}; Number2 num2[]={ -,Ten, About,7, the, A, $, the,3, -, the,0}; intMain () {priority_queue<int>que;//construct queues with default precedencepriority_queue<int,vector<int>,cmp1>que1;//Minimum value Firstpriority_queue<int,vector<int>,cmp2>que2;//Maximum Value Firstpriority_queue<int,vector<int>,greater<int> >que3;//Note that ">>" will be considered an error,//This is the right-shift operator, so it's separated by a space number.priority_queue<int,vector<int>,less<int> >que4;////MAX Prioritypriority_queue<number1>Que5; Priority_queue<number2>Que6; inti;  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 ("take the default priority relationship: \ 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 ("priority is defined in the header file \ "functional\": \ 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 mode two: \ 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: with default precedence: (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 91Queue 2:83 72 56 47 36 22 14 10 7 3 with header file "funct Ional "within defined priority: (priority_queue<int,vector<int>,greater<int>/less<int> >que;) Queue 3:7 10 14 91Queue 4:83 72 56 47 36 22 14 10 7 3 Adopt structure custom Priority Mode II: (priority_queue<number>que) Queue 5:7 10 1 4-91Queue 6:83 (7 3 )--*/ 

c++stl--Priority Queue

Related Article

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.