C++queue Container Learning (detailed)

Source: Internet
Author: User

Tag: class file in file comparison operator PAC HTTP output data priority

A. 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 element type, one 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. Queue: As Q.push (x): The x element is connected to the end of the line;

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

3, access to the first element of the team: such as Q.front ()

4, access to the 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 priority queue and the queue is that the priority queue is not out of line in the queue, but rather in the order of priority of the elements in the queues (the default is the big priority, You can also specify your own precedence by specifying operators).

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 a vector, the default operator is less, that is, a small forward row, large back row (out of the queue when the tail of the elements out of the 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;//Define a small first-out team

Priority_queue basic operations are the same as the queue

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

Let's look at this simple example:

1#include <iostream>2#include <queue>3#include <stdlib.h>4 using namespacestd;5 classT6 {7  Public:8     intx, y, z9TintAintBintc): X (a), Y (b), Z (c)Ten     { One     } A }; - BOOL operator< (ConstT&AMP;T1,Constt&T2) - { the     returnt1.z<t2.z; - } - intMainvoid) - { +Priority_queue<t>Q; -Q.push (T (4,4,3)); +Q.push (T (2,2,5)); AQ.push (T (1,5,4)); atQ.push (T (3,3,6)); -      while(!q.empty ()) -     { -T t=q.top (); - Q.pop (); -cout<<t.x<<" "<<t.y<<" "<<t.z<<Endl; in     } -System"Pause"); to     return 1; +}

The output result is

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

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

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

{

Return t1.z>t2.z;

}

Then the resulting output will be in the order of z from small to large team.

C++queue Container Learning (detailed)

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.