Introduction to queue priority queue (priority_queue)

Source: Internet
Author: User
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

}

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.