C++queue Container Introduction

Source: Internet
Author: User

The definition of the queue template class is in the <queue> header file.

The queue is very similar to the stack template, and the queue template also needs to define two template parameters, one is the element type, one is the container type, the element type is necessary, the container type is optional, and the default is the Dqueue type.

The sample code that defines the queue object is as follows:

queue<int>q1;

queue<double>q2;

The basic operations of the queue are:

1. Team: such as Q.push (x): The x element is received at the end of the queue;

2. Out team: The first element of the pop-up queue, such as Q.pop (), does not return the value of the element;

3, visit team first element: such as Q.front ()

4, access to team tail elements, such as q.back ();

5, the number of elements in the access team, such as Q.size ();

Two. Priority queue

In the <queue> header file, a very useful template class Priority_queue (priority queue) is also defined, and the difference between the precedence queue and the queue is that the priority queue does not team out in the queue, but rather follows the priority order of the elements in the queues (the default is the large priority, You can also specify your own order of precedence by specifying an operator.

The Priority_queue template class has three template parameters, element types, container types, and comparison operators. The latter two can be omitted, the default container is vector, the default operator is less, that is, small forward row, large back row (the elements of the end of the queue out team).

The sample code that defines the Priority_queue object is as follows:

Priority_queue<int >q1;

Priority_queue<pair<int,int> >q2;

priority_queue<int,vector<int>,greater<int> >q3;//definition Small First out team

Priority_queue basic operations are the same as the queue

The most difficult thing for beginners to do when using Priority_queue is to define the comparison operator. If it is a basic data type, or a class that has a comparison operator defined, you can use the less operator and the greater operator of the STL directly-default to use the less operator, that is, the small forward row, the large first team. If you want to define your own comparison operators, there are several ways to do this, one of which is the overloaded comparison operator. The precedence queue attempts to substitute two elements x and y into the comparison operator (for the less operator, call X<y, the greater operator, call X>y), if the result is true, then x row in front of y, Y will precede X team, on the contrary, the Y row in front of X, X will first out team.

Look at the following simple example:

#include <iostream>
#include <queue>
#include <stdlib.h>
using namespace std;
Class T
{public
:
	int x,y,z;
	T (int a,int b,int C): X (a), Y (b), Z (c)
	{
	}
};
BOOL operator< (const t&t1,const t&t2)
{return
	t1.z<t2.z;
}
int main (void)
{
	priority_queue<t>q;
	Q.push (T (4,4,3));
	Q.push (T (2,2,5));
	Q.push (T (1,5,4));
	Q.push (T (3,3,6));
	while (!q.empty ())
	{
		T t=q.top ();
		Q.pop ();
		cout<<t.x<< "" <<t.y<< "" <<t.z<<endl;
	}
	System ("Pause");
	return 1;
}


Output results are

Note that this is in Z's order from big to small out of the team.

If we change the comparison operator overload in the example above to read:

BOOL operator< (const T &t1,const t &t2)

{

Return t1.z>t2.z;

}

The resulting output will be in Z's order from small to large teams.


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.