No, no. The statement is required.
C ++ STL priority_queue Learning
This article is excerpted from the Internet, so learning is recorded here.
The header file containing priority_queue is <queue>
Primary member of priority_queue class:
Priority_queue (); // default constructor to generate an empty sorting queue
Priority_queue (const queue &); // copy the constructor
Priority_queue & operator = (const priority_queue &); // The Value assignment operator is overloaded.
Private member of priority_queue:
Value_type; // The object type stored in priority_queue, which is the same as the T type in priority_queue.
Priority_queue (const compare & Comp); // construct an empty priority_queue object and use comp as the comparison of priority_queue
Priority_queue (const value_type * First, const value_type * Last); // constructor with two parameters, using the default comparison as the third parameter
Size_type; // positive integer type, which is the same as sequence: size_type.
Bool empty () const; // determines whether the priority queue is null. If it is null, true is returned; otherwise, false is returned.
Size_type size () const; // returns the number of elements in the priority queue.
Const value_type & Top () const (); // return the reference value of the first element in the priority queue.
Void push (const value_type & X); // insert element X to the end of the priority queue. the queue length is increased by 1.
Void POP (); // Delete the first value of the priority queue, provided that the queue is not empty. After deletion, the queue length is reduced by 1.
Priority_queue <type, container, functional>
If we set the following two parameters by default, the priority queue is a large top heap with the largest element in the header. (This can be seen from the above Program)
Parameter |
Description |
Default |
T |
The type of object stored in the priority queue. |
|
Sequence |
The type of the underlying container used to implement the priority queue. |
Vector <t> |
Compare |
The comparison function used to determine whether one element is smaller Another element. If comparex, Y) is true, then X is smaller than Y. The element Returned by Q. Top) is the largest element in the priority queue. That is, it has Property that, for every other element x in the priority queue, compare (Q. Top (), X) is false. |
Less <t> |
After the operator is reloaded for the custom type, only one template parameter can be included when the object is declared.
But it cannot be declared like the basic type.
Priority_queue <node, vector <node>, greater <node>;
The reason is that greater <node> is not defined. If you want to use this method to define
You can use the following method:
# Include <iostream>
# Include <queue>
Using namespace STD;
Struct Node
{
Int X, Y;
Node (int A = 0, int B = 0): X (A), y (B ){}
};
Struct CMP
{
Bool operator () (node A, Node B ){
If (A. X = B. X)
Return A. Y> B. Y;
Return A. x> B. X;
}
};
Int main (){
Priority_queue <node, vector <node>, CMP> q;
For (INT I = 0; I <10; ++ I)
{
Q. Push (node (RAND (), Rand ()));
}
While (! Q. Empty ())
{
Cout <q. Top (). x <"" <q. Top (). Y <Endl;
Q. Pop ();
}
Return exit_success;
}
In fact, there are three usage methods.
First, use the default one directly.
Its template declaration has three parameters: priority_queue <type, container, functional>
Type is the data type, container is the container for storing data, and functional is the element comparison method.
The container must be a container implemented using arrays, such as vector and deque, but not list.
In STL, vector is used by default. Operator is used by default for comparison.
If the parameter is set to the default value, the priority queue is the top heap with the largest element in the header.
Look at the example
Priority_queue <int> qi;
Int A [Len] = {3, 5, 9, 6, 2 };
Priority_queue <int> qi;
For (I = 0; I <Len; I ++)
Qi. Push (A [I]);
For (I = 0; I <Len; I ++)
{
Cout <QI. Top () <"";
Qi. Pop ();
}
The <operator indicates that the priority of a large element in an integer is high.
In this example, the output result is 9 6 5 3 2.
Second:
Method 2:
In example 1, what if we want to output elements from small to large?
In this case, we can pass in a comparison function using the functional. H function object as the comparison function.
If you want to use a small top heap, you must include all three parameters in the template.
STL defines a function like greater. for basic types, you can use this function to declare a small top heap.
Priority_queue <int, vector <int>, greater <int> qi2;
For a custom type, you must reload the operator yourself <or write the imitation function yourself.
# Include <iostream>
# Include <queue>
Using namespace STD;
Struct node {
Int X, Y;
Node (int A = 0, int B = 0 ):
X (A), y (B ){}
};
Bool operator <(node A, Node B ){
If (A. X = B. X) Return A. Y> B. Y;
Return A. x> B. X;
}
Int main (){
Priority_queue <node> q;
For (INT I = 0; I <10; ++ I)
Q. Push (node (RAND (), Rand ()));
While (! Q. Empty ()){
Cout <q. Top (). x <''<q. Top (). Y <Endl;
Q. Pop ();
}
Getchar ();
Return 0;
}
Or the definition can achieve the effect:
Struct node {
Int X, Y;
Node (int A = 0, int B = 0 ):
X (A), y (B ){}
Friend operator <(node A, Node B ){
If (A. X = B. X) Return A. Y> B. Y;
Return A. x> B. X;
}
};
After the operator is reloaded for the custom type, only one template parameter can be included when the object is declared.
But it cannot be declared like the basic type.
Priority_queue <node, vector <node>, greater <node>;
The reason is that greater <node> is not defined. If you want to use this method to define
The method is as follows:
Example:
# Include <iostream>
# Include <queue>
Using namespace STD;
Struct node {
Int X, Y;
Node (int A = 0, int B = 0 ):
X (A), y (B ){}
};
Struct CMP {
Bool operator () (node A, Node B ){
If (A. X = B. X) Return A. Y> B. Y;
Return A. x> B. X ;}
};
Int main (){
Priority_queue <node, vector <node>, CMP> q;
For (INT I = 0; I <10; ++ I)
Q. Push (node (RAND (), Rand ()));
While (! Q. Empty ()){
Cout <q. Top (). x <''<q. Top (). Y <Endl;
Q. Pop ();
}
Getchar ();
Return 0;
}
Note that the three parameters in priority_queue can be omitted because there are default parameters. However, if there is a third parameter, the second parameter must be written.