C + + Road set sail--Standard Template Library (queue)

Source: Internet
Author: User

Queue

FIFO queue: First in-line queue.

Priority queue: Outputs the priority size of the elements in the queue.

Defined:

FIFO Queue: queue< data class > variable name.

Priority queue:priority_queue< data type > variable name.

Eg:

FIFO queue: queue<int>que//defines a FIFO queue that is an integral type named Que.

Priority queue: priority_queue<int>que//defines a priority queue for an integral type named Que.

Overloaded operators are required when the type is a custom structure.

Eg:

struct ss//defines the structure of a SS
{
int x, y;
SS () {}//constructor
SS (int xx,int yy)//overloaded function
{
X=xx;
Y=yy;
}
BOOL operator< (const ss& B) const{//overloaded function operator.
Return y<b.y;
}
};

priority_queue<ss>que;//definition use;

Note: When invoking an action function, the type should also be a defined type, as the example can be manipulated with Que.push (SS) because it overloads the initial function.

Basic operation:

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

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

Que.pop ()//delete the first element of the team, but not return its value;

Que.front ()//Returns the value of the first element of the team, but does not delete the element (only for FIFO queues)

Que.back ()//Returns the value of the tail element of the queue, but does not delete the element (only for FIFO queues)

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

Q.push ()//on queue; Press a new element at the end of the team; for Priority_queue, insert a new element 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);
Que1.push (SS (2,3));
Que1.push (SS (3,4));
cout<< "Length of que1";
Cout<<que1.size ();
cout<< "The first element in the Que2";
Cout<<que2.front () <<endl;
cout<< "Delete 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 the que1";
Cout<<que1.top () .x<< "" <<que1.top () .y<<endl;
}

C + + Road set sail--Standard Template Library (queue)

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.