STL queue, priority queue, and stack

Source: Internet
Author: User

How to Use the priority queue in STL (priority_queu)

Basic operations:

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, a queue with a higher priority is displayed first. In the default int type, a large number is displayed first.

Usage:

Header file:

# Include <queue>

Declaration method:

1. Common Methods:

Priority_queue <int> q;
// The elements are displayed in the ascending order of the elements.

2. Custom priority:

Struct CMP
{
Operator bool () (int x, int y)
{
Return x> Y; // a smaller value of X has a higher priority.
// Other methods can also be written, such as return P [x]> P [y]; indicating that P [I] has a low priority.
}
};
Priority_queue <int, vector <int>, CMP> q; // define a method
// The second parameter indicates the container type. The third parameter is the comparison function.

3. struct declaration method:

Struct Node
{
Int X, Y;
Friend bool operator <(node A, Node B)
{
Return A. x> B. X; // In the struct, the priority of X is higher.
}
};
Priority_queue <node> q; // define a method
// In this structure, Y is the value, and X is the priority.
// Compare the priority of an element using the custom operator <operator.
// When "<" is reloaded, it is best not to reload ">", which may cause compilation errors.

 

Queue usage in STL (Queue)

Basic operations:

Push (x) pushes X to the end of the queue

Pop () pops up the first element of the queue (the top element of the queue). Note that this function does not return any value.

Front () returns the first element (top element)

Back () returns the final pressed element (team end element)

Empty () returns true if the queue is empty

Size () returns the length of the queue.

 

Usage:

Header file:

# Include <queue>
Declaration method:

1. General statement

Queue <int> q;


2. struct

Struct Node
{
Int X, Y;
};
Queue <node> q;

 

Stack usage in STL)

Basic operations:

Push (x) adds X to the stack, that is, the inbound operation.

Pop () Out stack operation (delete stack top), only out stack, no return value

Top () returns the first element (the top element of the stack)

Size () returns the number of elements in the stack.

Empty () returns true if the stack is empty


Usage:

Similar to the queue, the header file is:

# Include <stack>


Definition method:

Stack <int> S1; // The inbound stack element is of the int type.
Stack <string> S2; // The queuing element is string type.
Stack <node> S3; // The queue entry element is custom

Q4.pop ();
}
// Cout <Endl;
}
Return 0;
} URL: http://www.cppblog.com/CodeStream/archive/2011/03/25/142700.html

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.