Priority_queue adapter for STL Learning

Source: Internet
Author: User

Priority_queue
The priority queue is a container adapter type. Based on some strict weak sorting conditions, a container with the maximum elements of the first element is specially designed.

Like a heap, you can retrieve the largest heap element (the top element in the priority queue) and insert unlimited elements.

The priority queue acts as the container adapter and uses a specific container class's encapsulated object as its underlying container. It provides a set of member functions that access container elements. All elements are pop-up operations from the "tail" of a specific container. This "tail" is called the top of the priority queue.

The underlying container can be any standard container class template or other specific design container class. The only requirement is to support the following operations:

Front ()
Push_back ()
Pop_back ()
Therefore, you can use standard container class templates such as vector and deque. By default, if no container class is specified for a special priority_queue class, the vector standard class template is used.
To keep the internal structure of the heap at all times, random access to the iterator must be supported. When appropriate, the container adapter automatically executes the push_heap and pop_heap algorithms by calling make_heap.
In the definition of the c ++ standard template class, priority queue has three templates for participation:
[Cpp]
Template <class T. class Cotainer = vector <T>,
Class Compare = less <typename Container: value_type> class priority_queue;
Parameters:
T: Element type.
Container: The underlying Container object type used to store and access elements.
Compare: Comparison class: com (a, B) Class. comp is an object of this class and a and B are elements of the container. If a is placed in the container before B, true is returned. This can be a class that executes the function call operation or a pointer to the function. The default value is less <T>, and the less <T> return value is similar to the (a <B) operation. The priority_queue object uses this expression when an element is inserted or deleted to ensure that the element popped up in the priority queue is always the largest element.
Priority_queue: priority_queue
[Cpp] view plaincopy
Explicit priority_queue (const Compare & x = Compare (), const Container & y = Container ());
Template <class InputIterator>
Priority_queue (InputIterator first, InputIterator last, const Compare & x = Compare (), const Container & y = Container ());
Construct a priority_queue container adapter object.
The container adapter treats the container object as data. If yes, the container object is a copy of Parameter y; otherwise, it is an empty container.
Priority_queue satisfies the heap priority (the pop-up element is always the largest element in the container) operation, but the exact sequence criterion may be modified due to the appropriate y parameter.
(Because priority_queues satisfy the heap property the element popped from it is always the highest in the container. but the exact ordering criterium to determine this may be any that follow a strict weak ordering, which may be modified with an appropiatey parameter.
).
Parameters
X
Comparision objects that are strictly sorted.
Y
Container object.
The container is the second type of template parameter (the underlying container type provided by priority_queue). The default value is vector <T>.
First, last
The input iterator at the first and last positions in a sequence uses the [first, last) interval.
Example:
[Cpp]
# Include <iostream>
# Include <queue>
 
Using namespace std;
Class MyCmp
{
Bool reverse;
Public:
MyCmp (const bool & revParam = false)
{
Reverse = revParam;
}
Bool operator () (const int & lhs, const int & rhs) const
{
If (reverse) return (lhs <rhs );
Else return (lhs <rhs );
}
};
 
Int main (void)
{
Int myInts [] = {10, 20, 60, 15 };

Priority_queue <int> first;
Priority_queue <int> second (myInts, myInts + 4 );
Priority_queue <int, vector <int>, greater <int> third (myInts, myInts + 4 );
 
Priority_queue <int, vector <int>, MyCmp> fourth;

Typedef priority_queue <int, vector <int>, MyCmp> mypq_type;
Mypq_type th (MyCmp ());
Mypq_type sixth (MyCmp (true ));
 
Return 0;
}
No execution result.
Priority_queue: top
[Cpp] view plaincopy
Const value_type & top () const;

Returns a common reference of the most "TOP" element of priority_queue. This "TOP" element is a relatively high (compares higher) element in priority_queue, And the element popped up when priority_queue: pop is called.
This member function actually calls the fron member function of the bottom container object.
Example:
[Cpp]
# Include <iostream>
# Include <queue>
 
Using namespace std;
 
Int main (void)
{
Priority_queue <int> myqu;

Myqu. push (10 );
Myqu. push (20 );
Myqu. push (15 );
 
While (! Myqu. empty ())
{
Cout <"myqu. top () is now" <myqu. top () <endl;
Myqu. pop ();
}
Return 0;
}
Execution result:
[Cpp]
Www.2cto.com
Myqu. top () is now 20
Myqu. top () is now 15
Myqu. top () is now 10
It can also be seen from the program that pop execution also pops up in the order of sorting.

Other member functions (similar to queue, do not repeat them ):
Empty
Test whether the container is empty.
Size
The size of the returned container.
Push
Insert element.
Pop
Pop-up element.
By richerg85

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.