Queue queue container

Source: Internet
Author: User
Queue queue container

The queue container is a first-in-first-out linear storage table. Element insertion can only be performed at the end of the queue, and element deletion can only be performed at the beginning of the queue.

To use queue, You need to declare the header file "# include <queue>"

Push (): enters the queue, that is, inserts an element.

Pop (): leaves the queue, that is, deleting elements

Front (): Read the first element of the queue // POP () in the priority queue ();

Back (): reads team-end Elements

Empty (): determines whether the queue is empty

Size (): Current element of the queue

Example:

# Include <queue>

# Include <iostream>

Using namespace STD;

Int main ()

{

// Define a queue

Queue <int> q;

// Queue, that is, insert an element

Q. Push (1 );

Q. Push (2 );

Q. Push (3 );

Q. Push (9 );

// Returns the number of team instance Elements

Cout <q. Size () <Endl;

// Determine whether the queue is empty

Cout <q. Empty () <Endl;

// Read the first element of the team

Cout <q. Front () <Endl;

// Read the team End Element

Cout <q. Back () <Endl;

// Delete all elements from the column

While (Q. Empty ()! = True)

{

Cout <q. Front () <"";

// Delete the first element of the team

Q. Pop ();

}

Cout <Endl;

Return 0;

}

Priority_queue

Like a queue, a priority queue container can only Insert elements from the end of a queue and delete elements from the first queue. However, it has a feature that the largest element in the queue is always at the top of the queue. Therefore, when leaving the queue, it does not follow the principle of first-in-first-out, but leaves the largest element in the current queue. This is similar to sorting the elements in the queue in a descending order. By default, the comparison rules of elements are sorted by element values in ascending order. You can reload the <operator to redefine the comparison rules.

When using the priority queue, you also need to declare the header file "# include <queue>"

# Include <iostream>

# Include <queue>

Using namespace STD;

Int main ()

{

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;

}

If the element type of the priority queue is struct, You can reload the <"operator in the struct to modify the priority of the priority queue.

# Include <queue>

# Include <string>

# Include <iostream>

Using namespace STD;

// Define struct

Struct info

{

String name;

Float score;

Bool operator <(const info & A) const

{

// Sort the score from small to large. If you want to use the score, use "> ".

Return A. score <score;

}

};

Int main ()

{

Priority_queue <info> PQ;

Info in;

In. Name = "Jack ";

In. Score = 68.5;

PQ. Push (in );

In. Name = "Bomi ";

In. Score = 18.5;

PQ. Push (in );

In. Name = "peti ";

In. Score = 90;

PQ. Push (in );

While (! PQ. Empty ())

{

Cout <PQ. Top (). Name <":" <PQ. Top (). score <Endl;

PQ. Pop ();

}

Return 0;

}

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.