STL priority_queue (priority queue) detailed _c language

Source: Internet
Author: User

Construction, deconstruction

Priority_queue ()//default constructor to generate an empty sort queue 

priority_queue (const queue&);//copy constructor 

priority_queue (const compare& comp);
Construct to generate an empty Priority_queue object,
///Use comp as priority_queue comparison 

priority_queue (const value_type* Const value_type* last); 
A constructor with two parameters 
///using the default comparison as the third parameter 

priority_queue& operator= (const priority_queue &);  
The assignment operator Overloads 

C.~priority_queue ()//destroys all elements and frees memory 

### Common Function ###

Empty ()//judge whether it is empty 

push (Elem e);//queue tail adds one element 

pop ();//Queue header data out team top 

()//return header data 

size ()//return number of elements in stack 

### Change the arrangement order ###
Priority_queue < Type, Container, Functional >
If we default on the next two parameters, the priority queue is the big top heap,
The team head element is the largest. In many cases, what we need is not necessarily the maximum value,
It is also possible to have a minimum value. This is what we need to change the order in the priority_queue.

There are two kinds of methods:
1. If the priority queue is a basic type, then we can do so, we take int as an example:

Note that there is a space between greater<int> >
Priority_queue<int, Vector<int>, greater<int> >Q;

2. For custom data types, whether we want to change the sort method or not, we need to-overload less than (<) operators:
Because, if you do not overload the comparison operator, the compiler cannot compare the size relationship of the custom data type. However, because within the priority_queue, we only need to use the less than sign (<), so we simply overload the less than number. Of course, for custom data types, you must also overload, otherwise you will not be able to use priority_queue. Overload is less than the number, we can have two ways, a member function, a kind of make Ufida function (here is not much said, not the classmate, oneself in a good review review C + +)

Example of ### priority queue usage ###

#include <queue> 
#include <list> 
#include <cstdio> 
using namespace std; 
/** 
 Priority Queue Use example 
* 
/int main () { 
  //Precedence queue default is to use vector as container 
  priority_queue<int> A; 
  Priority_queue<int,list<int>> b;//can be defined in this way, but cannot use 
  int i; 
  Press into data for 
  (i=0;i<10;i++) { 
   a.push (i*2-5); 
   B.push (i);//Compile Error 
  } 
  printf ("Priority queue Size =%d\n", a.size ()); 
  while (!a.empty ()) { 
    printf ("%d\n", A.top ()); 
    A.pop ()//OUT Team 
  } 
  putchar (' \ n '); 
  return 0; 
} 

### Priority queue with comparison function example (for struct) ###

The following program is for the structural body, the comparison of the data is by the weight of the structure operator ().
program function is to simulate the queue process, each has a name and priority, the same priority is the comparison of the name, the beginning of 5 people into the queue, and then the team's first 2 people out of the team, and then 3 people into the queue, and finally everyone in turn, the program will output the order

#include <queue> #include <cstring> #include <cstdio> using namespace std; 
    /** structure Body/struct node{char szname[20];//person name int priority;//Priority//constructor Node (int nri, char *pszname) { 
    strcpy (SzName, pszname); 
  Priority = NRI; 
} 
}; 
  Comparison method of/** structure rewrite operator ()/struct nodecmp{//rewrite operator () method, note the rewrite here, operator () (parameter 1, ...) BOOL Operator () (const node &na, const node &AMP;NB) {if (na.priority!= nb.priority) return na.priority &L 
    T;= nb.priority; 
  else return strcmp (Na.szname, nb.szname) > 0; 
} 
}; 
/** Print node */void Printfnode (node na) {printf ("%s%d\n", Na.szname, na.priority); 
  the int main () {//Priority queue defaults to using vector as a container and the underlying data structure is a heap. 

  Priority_queue<node, Vector<node>, nodecmp> A; 
  There are 5 people entering the queue A.push (Node (5, "Xiao Tan")); 
  A.push (Node (3, "Xiao Liu")); 
  A.push (Node (1, "Xiao Tao")); 

  A.push (Node (5, "Xiao Wang")); 
  2 Men of the team head out Team Printfnode (A.top ()); 
  A.pop (); 
  Printfnode (A.top ()); 
  A.pop (); PrinTF ("--------------------\ n"); 
  Then enter 3 persons A.push (Node (2, "small white")); 
  A.push (Node (2, "cockroach")); 

  A.push (Node (3, "small New")); 
    Everyone in turn is out of the team while (!a.empty ()) {Printfnode (A.top ()); 
  A.pop (); 
return 0; 

 }

Thank you for reading, I hope to help you, thank you for your support for this site!

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.