Queue)
We call queue a container adapter because it is implemented through deque, which is equivalent to a deque with weaker functionality.
The main functions are as follows:
Empty ()Returns true if the queue is empty.
Pop ()Delete top Element
Push ()Add an element
Size ()Returns the number of elements in the queue.
Back ()Returns the end element of the queue.
Front ()Return the queue Header element.
We know that the queue is characterized by first-in-first-out, push, adding elements to the backend, and removing elements from the POP header,
Of course, the first element is obtained through front () before deletion.
Example:
# Include <queue>
Using namespace STD;
Queue <int> qu;
Bool isempty = Qu. Empty (); // if it is null, true is returned.
Qu. Push (11 );
Qu. Push (22 );
Int head = Qu. Front (); // 11
Int end = Qu. Back (); // 22
Qu. Pop (); // Delete the Header element 11
Priority_queue)
Priority_queue is also an adapter container, which is implemented through vector. It is essentially a heap ).
It also has the highest priority, not first-in-first-out.. Priority_queue is a large heap by default.
Main member functions
Empty () returns true if the queue is empty
Pop () deletes the peer Element
Push () to add an element
Size () returns the number of elements in the priority queue.
Top () returns the top-to-top element of the priority queue.
In the default priority queue
Example:
# Include <queue>
Using namespace STD;
Priority_queue <int> QQ;
QQ. Push (2 );
QQ. Push (5 );
QQ. Push (3 );
QQ. Push (4 );
While (! QQ. Empty ())
{
Cout <QQ. Top () <Endl; // The result is 5 4 3 2 in the ascending order..
QQ. Pop (); // Delete the elements in the queue Header
}