How to use the queue in STL

Source: Internet
Author: User

Use of STL Squadron (queue)

Basic operation:

Push (x) presses x into the end of the queue

Pop () pops the first element of the queue (the top element of the team), noting that this function does not return any values

Front () returns the first element (top element of the team)

Back () returns the element (tail element) that was last pressed in

Empty () returns True when the queue is empty

Size () returns the length of the queue

How to use:

Header file:

#include <queue>

Declaration method:

1. General Declaration

queue<int>q;

2. Structural body

struct node{int x, y;};queue<node>q;

How to use priority Queues in STL (Priority_queu)

The priority queue container, like a queue, can only insert elements from the end of a team and delete elements from the first team. But it has a feature that the largest element in the queue is always at the head of the team, so when the team is out, it does not follow the FIFO principle, but the largest element in the current queue. This is similar to the order in which the elements in the queue are sorted by the small size of each other. The comparison rules for elements by default are sorted by element values from large to small, and the<operator can be overloaded toredefine the comparison rule.   

Basic operation:

Empty () returns true if the queue is null

Pop () Delete the top element

Push () joins an element

Size () returns the number of elements owned in the priority queue

Top () returns priority queue to top element

In the default priority queue, high priority first-out team. In the default int type, the first team is the larger number.

How to use:

Header file:

#include <queue> 

How to declare:

1. Common method:

priority_queue<int>q;By manipulating the elements from the large to the small order of the team

2. Custom Priority:

struct CMP {operator bool (int x, int y) {return x > y;//x Small priority high//can also be written in other ways, such as: RET Urn P[x] > p[y]; p[i] small high Priority}};Priority_queue<int, Vector<int>, cmp>q;Define the method//where the second parameter is a container type. The third parameter is a comparison function. 

3, the structure of the Declaration method:

struct node {int x, y;    friend bool Operator < (Node A, Node B) {return a.x > b.x; structure, x small high Priority}};priority_queue<node>q;Define the method//in the structure, Y is the value and X is the priority. The priority in the element is compared by customizing the operator< operator. It is best not to overload ">" When Overloading "<", a compilation error may occur

How to use the queue in STL

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.