The path to C ++-standard template library (queue ),

Source: Internet
Author: User

The path to C ++-standard template library (queue ),

Queue:

FIFO queue: first-in-first-out queue.

Priority queue: outputs the priority of the elements in the queue.

Definition:

FIFO queue: The <data type> variable name of the queue.

Priority queue: priority_queue <data type> variable name.

Eg:

FIFO queue: queue <int> que // defines a first-in-first-out queue named que.

Priority queue: priority_queue <int> que // defines an integer priority queue named que.

// When the type is a custom structure, you need to overload the operator.

Eg:

Struct ss // defines an ss struct
{
Int x, y;
Ss () {}// Constructor
Ss (int xx, int yy) // overload Function
{
X = xx;
Y = yy;
}
Bool operator <(const ss & B) const {// overload function operators.
Return y <B. y;
}
};

Priority_queue <ss> que; // defines the usage;

Note: When calling an operation function, the type should also be the defined type. In this example, because the initial function is overloaded, you can use que. push (ss (1, 2) for the operation.

Basic operations:

Que. empty () // If the queue is not empty, false is returned; otherwise, true is returned;

Que. szie () // returns the number of elements in the queue;

Que. pop () // Delete the first element of the team, but no value is returned;

Que. front () // return the value of the first element of the queue, but this element is not deleted (only applicable to FIFO queues)

Que. back () // returns the value of the team end element, but does not delete the element (only applicable to FIFO queues)

Q. top () // returns the value of the element with the highest priority, but does not delete this element (applicable only to priority queue)

Q. push () // to the queue; press a new element at the end of the team; Insert a new element to the priority_queue at the current position based on the priority.

Code explanation:

# Include <cstdio>
# Include <queue>
# Include <iostream>

Using namespace std;

Struct ss
{
Int x, y;
Ss (){}
Ss (int xx, int yy)
{
X = xx;
Y = yy;
}
Bool operator <(const ss & B) const {
Return y <B. y;
}
};
Int main ()
{
Priority_queue <ss> que1;
Queue <int> que2;
Que2.push (1 );
Que2.push (3 );
Que2.push (2 );
Cout <"length of que2 ";
Cout <que2.size () <endl;
Que1.push (ss (1, 2 ));
Que1.push (ss (2, 3 ));
Que1.push (ss (3, 4 ));
Cout <"length of que1 ";
Cout <que1.size ();
Cout <"the first element in que2 ";
Cout <que2.front () <endl;
Cout <"Delete the first element in que2" <endl;
Que2.pop ();
Cout <"length of que2 ";
Cout <que2.size () <endl;
Cout <"x and y values of the first element of que1 ";
Cout <que1.top (). x <"" <que1.top (). y <endl;
}

Related Article

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.