Priority_queue
Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition.
This context is similar to a heap where only the max heap element can be retrieved (the one at the top in the priority queue) and elements can be inserted indefinitely.
Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific iner class as its underlying container, providing a specific set of member functions to access its elements. elements are popped from the "back" of the specific iner, which is known as the top of the priority queue.
The underlying container may be any of the standard container class templates or some other specifically designed container class. the only requirement is that it must be accessible through random access iterators and it must support the following operations:
Front ()
Push_back ()
Pop_back ()
Therefore, the standard container class templates vector and deque can be used. By default, if no container class is specified for a particle priority_queue class, the standard container class template vector is used.
Support for random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by calling the algorithms make_heap, push_heap and pop_heap when appropriate.
In their implementation in the C ++ Standard Template Library, priority queues take three template parameters:
1
2
Template <class T, class Container = vector <T>,
Class Compare = less <typename Container: value_type> class priority_queue;
Where the template parameters have the following meanings:
T: Type of the elements.
Container: Type of the underlying container object used to store and access the elements.
Compare: Comparison class: A class such that the expression comp (a, B), where comp is an object of this class and a and B are elements of the container, returns true if a is to be placed earlier than B in a strict weak ordering operation. this can either be a class implementing a function call operator or a pointer to a function. this defaults to less <T>, which returns the same as applying the less-than operator (a <B ).
The priority_queue object uses this expression when an element is inserted or removed from it (using push orpop, respectively) to grant that the element popped is always the greatest in the priority queue.
In the reference for the priority_queue member functions, these same names (T, Container and Compare) are assumed for the template parameters.
Member functions
(Constructor)
Construct priority queue (public member function)
Empty
Test whether container is empty (public member function)
Size
Return size (public member function)
Top
Access top element (public member function)
Push
Insert element (public member function)
Pop
Remove top element (public member function)
Priority_queue <Elem> c
Create an empty queue.
Note: The priority_queue constructor has 7 versions. Please refer to the MSDN
C. top ()
Back to queue header data
C. push (elem)
Add elem data at the end of the queue
C. pop ()
Data in the queue header goes out of the queue
C. empty ()
Determine whether the queue is empty
C. size ()
Returns the number of data in the queue.
A priority queue container is also a queue that queues one end and the other end. Different from the general queue, the largest element in the queue is always at the first position of the queue. Therefore, the element is not in line with the requirements of first-in-first-out, and the element that is first in the queue is in line, instead, the maximum element in the current queue is aligned. The generalization of the C ++ STL priority queue. By default, the vector container is used at the underlying layer, so that the elements of the queue container can be used for Array Operations, and the heap algorithm is used to locate the maximum element of the current queue, and adjust it to the first position of the team to ensure that the maximum element is displayed. Heap algorithm (heap algorithm) has nLog (n) algorithm time complexity. In addition, the priority queue can also be considered as a container adapter, which converts the bottom-layer serial container vector to the priority queue priority_queue.
The C ++ standard header file of the priority_queue priority queue container is also queue, which must be included in the macro statement "# include <queue>.
The same reason is that only operations on the first and last elements are required. Therefore, the priority_queue priority queue container does not provide an iterator to directly access the elements at any other location. Generally, priority_queue <T> is used to present data. T is a type of priority queue element.
Create a priority_queue object
Before using the priority_queue queue, you must use the constructor to generate a priority object before you can perform operations such as element queuing, out-of-the-box operations, Team Leader operations, and team tail operations.
1. priority_queue ()
The default constructor creates an empty priority_queue object. For example, the following line of code uses the default vector as the underlying container and creates an empty priority queue object pq with the data element of the int type.
Priority_queue <int> pq;
2. priority_queue (const priority_queue &)
Copy the constructor to create a new priority queue object with a priority queue object. For example, the following code uses the priority_queue object pq1 to create a priority_queue object pq2 with a bidirectional linked list as the underlying container.
// Priority_queue <int, list <int> pq1;
Priority_queue <int, list <int> pq2 (pq1 );
Element queue
The element queuing function of the priority queue container is also a push function. It calls the heap algorithm function to move the elements in the queue to the correct position in the queue heap, so that the elements with the highest priority in the queue are always at the top of the queue. Priority queue does not preset a fixed size, so the push function does not judge whether the queue space is full, all elements are placed in the queue. The push function does not return the information about whether the element is successfully added to the queue.
Void push (const value_type & x)
Element pair
The element-to-function of the priority queue container is the pop function, removing the element with the highest priority. This function does not determine whether the queue is empty. It tries to delete the first element of the queue. In general, you need to judge that the queue is not empty before performing element-to-element operations.
First element
The top function of the priority queue container, which can be used to read the first element of the queue, that is, the element with the highest priority. This function actually calls the front function of the underlying container. Note that the priority queue container does not provide a function to obtain the elements at the end of the team. The following is the usage prototype of the top function.
Const value_type & top () const
Queue non-empty judgment
The empty function must be used to determine whether to enter the queue and whether the queue is empty. The following is the usage prototype of the empty function.
Bool empty ()
[Cpp]
# Include <iostream>
# Include <queue>
Using namespace std;
Void test_empty ()
{
Priority_queue <int> mypq;
Int sum (0 );
For (int I = 1; I <= 100; I ++) mypq. push (I );
While (! Mypq. empty ())
{
Sum + = mypq. top ();
Mypq. pop ();
}
Cout <"total:" <sum <endl;
} // Total: 5050
Void test_pop ()
{
Priority_queue <int> mypq;
Mypq. push (30 );
Mypq. push (1, 100 );
Mypq. push (25 );
Mypq. push (40 );
Cout <"Popping out elements ...";
While (! Mypq. empty ())
{
Cout <"" <mypq. top ();
Mypq. pop ();
}
Cout <endl;
} // Popping out elements... 100 40 30 25
Void test_top ()
{
Priority_queue <string> mypq;
Mypq. push ("how ");
Mypq. push ("are ");
Mypq. push ("you ");
Cout <"mypq. top () is now: --- >>>" <mypq. top () <endl;
} // Mypq. top () is now: --- >>> you
Int main ()
{
Test_empty ();
Cout <"\ n ********************************** * ************ \ n ";
Test_pop ();
Cout <"\ n ********************************** * ************ \ n ";
Test_top ();
Cout <"\ n ********************************** * ************ \ n ";
Priority_queue <float> q;
// Insert three elements into the priority queue
Q. push (66.6 );
Q. push (22.2 );
Q. push (44.4 );
// Read and print two elements
Cout <q. top () <'';
Q. pop ();
Cout <q. top () <endl;
Q. pop ();
// Insert three more elements
Q. push (11.1 );
Q. push (55.5 );
Q. push (33.3 );
// Skip one element
Q. pop ();
// Pop and print remaining elements
While (! Q. empty ())
{
Cout <q. top () <'';
Q. pop ();
}
Cout <endl;
}
/******************
Running result:
Total: 5050
**************************************** *******
Popping out elements... 100 40 30 25
**************************************** *******
Mypq. top () is now: --- >>> you
**************************************** *******
66.6 44.4
33.3 22.2 11.1
Process returned 0 (0x0) execution time: 0.055 s
Press any key to continue.
********************/
# Include <iostream>
# Include <queue>
Using namespace std;
Void test_empty ()
{
Priority_queue <int> mypq;
Int sum (0 );
For (int I = 1; I <= 100; I ++) mypq. push (I );
While (! Mypq. empty ())
{
Sum + = mypq. top ();
Mypq. pop ();
}
Cout <"total:" <sum <endl;
} // Total: 5050
Void test_pop ()
{
Priority_queue <int> mypq;
Mypq. push (30 );
Mypq. push (1, 100 );
Mypq. push (25 );
Mypq. push (40 );
Cout <"Popping out elements ...";
While (! Mypq. empty ())
{
Cout <"" <mypq. top ();
Mypq. pop ();
}
Cout <endl;
} // Popping out elements... 100 40 30 25
Void test_top ()
{
Priority_queue <string> mypq;
Mypq. push ("how ");
Mypq. push ("are ");
Mypq. push ("you ");
Cout <"mypq. top () is now: --- >>>" <mypq. top () <endl;
} // Mypq. top () is now: --- >>> you
Int main ()
{
Test_empty ();
Cout <"\ n ********************************** * ************ \ n ";
Test_pop ();
Cout <"\ n ********************************** * ************ \ n ";
Test_top ();
Cout <"\ n ********************************** * ************ \ n ";
Priority_queue <float> q;
// Insert three elements into the priority queue
Q. push (66.6 );
Q. push (22.2 );
Q. push (44.4 );
// Read and print two elements
Cout <q. top () <'';
Q. pop ();
Cout <q. top () <endl;
Q. pop ();
// Insert three more elements
Q. push (11.1 );
Q. push (55.5 );
Q. push (33.3 );
// Skip one element
Q. pop ();
// Pop and print remaining elements
While (! Q. empty ())
{
Cout <q. top () <'';
Q. pop ();
}
Cout <endl;
}
/******************
Running result:
Total: 5050
**************************************** *******
Popping out elements... 100 40 30 25
**************************************** *******
Mypq. top () is now: --- >>> you
**************************************** *******
66.6 44.4
33.3 22.2 11.1
Process returned 0 (0x0) execution time: 0.055 s
Press any key to continue.
********************/