Stack/queue usage

Source: Internet
Author: User

Stack/queue usage

Stack and queue are also frequently used data containers in program design. STL provides us with convenient
Stack queue implementation.
To be accurate, the stacks and queue in STL are different from those in containers such as vector and list, but are repackaged for these containers.
Here, we will not go into the Implementation Details of STL stack and queue, but learn about their basic usage.

1. Stack
The stack template class is defined in the <stack> header file.
The stack template class requires two template parameters: one is the element type and the other is the container type, but only the element type is necessary.
When the container type is not specified, the default container type is deque.
The sample code for defining a stack object is as follows:

Stack <int> S1;
Stack <string> S2;

Basic stack operations include:

Stack entry, for example, S. Push (X );

Out stack, for example, S. Pop (); note that the out stack operation only deletes the top element of the stack and does not return this element.

Top access stack, for example, S. Top ()

Judge whether the stack is empty. For example, S. Empty (). If the stack is empty, true is returned.

Number of elements in the access stack, for example, S. Size ()

2. Queue

The queue template class is defined in the <queue> header file.
Similar to the stack template class, the queue template class also requires two template parameters, one of which is the element type and the other is the container class.
Type, element type is necessary, container type is optional, the default is deque type.

The sample code for defining a queue object is as follows:

Queue <int> q1;

Queue <double> Q2;

Basic queue operations include:

Join the queue, for example, Q. Push (x); Connect X to the end of the queue.

For example, Q. Pop (); the first element of the queue is displayed. Note that the value of the element to be popped up is not returned.

The first element of the access queue, such as Q. Front (), which is the first element to be pushed into the queue.

Access the elements at the end of the team, for example, Q. Back (), that is, the elements that are finally pushed into the queue.

Determines whether the queue is empty. For example, Q. Empty (). If the queue is empty, true is returned.

Number of elements in the access queue, for example, Q. Size ()

3. priority_queue

In the <queue> header file, another useful template class priority_queue (priority queue) is also defined ). Priority team
The difference between a column and a queue is that the priority queue is not in the order of queuing, but in the priority order of the elements in the queue.
Out of the queue (the default value is greater than priority, or you can specify your own priority by specifying an operator ).

The priority_queue template class has three template parameters: the first is the element type, the second is the container type, and the third is the ratio
Operator. The first two can be omitted. The default container is vector, and the default operator is less, that is, the small front row, the large
(The elements at the end of the sequence are displayed ).
The sample code for defining the priority_queue object is as follows:

Priority_queue <int> q1;

Priority_queue <pair <int, int> Q2; // note that spaces must be left between two angle brackets.

Priority_queue <int, vector <int>, greater <int> Q3; // defines a small FIFO order.

The basic operation of priority_queue is the same as that of queue.

When Beginners use priority_queue, the most difficult thing is how to define a comparison operator.
If it is a basic data type or a class with a comparison operator defined, you can directly use the STL less operator and greater
Operator-the less operator is used by default, that is, the small one goes to the front row and the big one goes out first.
If you want to define your own comparison operators, there are multiple methods. Here we will introduce one of them: Reload comparison operators. Priority team
The column tries to substitute the two elements x and y into the comparison operator (for the less operator, call x <Y, for the greater operator, call X> Y ),
If the result is true, X is in front of Y, and Y is in front of X. Otherwise, Y is in front of X, and X is in the first place.
Let's look at the following simple example:

 

# Include <iostream> # include <queue> usingnamespacestd; classt {public: intx, Y, Z; t (INTA, intb, intc): X (), Y (B), Z (c) {}}; booloperator <(constt & T1, constt & T2) {returnt1.z <t2.z; // determine the sequence of T1 and T2 in Z order} Main () {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 () {TT = Q. top (); q. pop (); cout <t. x <"" <t. Y <"" <t. z <Endl;} return1 ;}

The output result is (from large to small in Z order ):
336
225
154
443
Let's look at an example from small to large teams in Z order:

 

#include<iostream>#include<queue>usingnamespacestd;classT{public:intx,y,z;T(inta,intb,intc):x(a),y(b),z(c){}};booloperator>(constT&t1,constT&t2){returnt1.z>t2.z;}main(){priority_queue<T,vector<T>,greater<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()){Tt=q.top();q.pop();cout<<t.x<<""<<t.y<<""<<t.z<<endl;}return1;}

Output result:
443
154
225
336
If we reload the comparison operator in the first example as follows:

Booloperator <(constt & T1, constt & T2)
{
Returnt1.z> t2.z; // determine the sequence of T1 and T2 in the Z sequence.
}

The program in the first example will get the same output result as the program in the second example.
1.6 nth_element specifies the sorting of Elements
Nth_element is a sort that is easy to understand but difficult to explain. The example is more convenient:
There are 10 students in the class. I want to know the number of students with the lowest scores.
If you want to meet the above requirements, you can use sort to sort the order, and then take the 4th bits (because it is from small to large), smarter
A friend will use partial_sort to rank only the first four digits and then get the first 4th digits. In fact, this is a waste, because the first two of you
There is no need for sorting. In this case, you need nth_element:

Template <classrandomaccessiterator>

Voidnth_element (randomaccessiteratorfirst, randomaccessiteratornth,
Randomaccessiteratorlast );

Template <classrandomaccessiterator, classstrictweakordering>

Voidnth_element (randomaccessiteratorfirst, randomaccessiteratornth,
Randomaccessiteratorlast, strictweakorderingcomp );

For the above instance requirements, you only need to modify the program in 1.4 as follows:

Stable_sort (vect. Begin (), vect. End (), less <student> ());

Replace:

Nth_element (vect. Begin (), vect. Begin () + 3, vect. End (), less <student> ());

The running result is:
------ Beforesort...
Tom: 74
Jimy: 56
Mary: 92
127y: 85
Jone: 56
Bush: 52
Winter: 77
Andyer: 63
Lily: 76
Maryia: 89
----- Aftersort ....
Jone: 56
Bush: 52
Jimy: 56
Andyer: 63
127y: 85
Mary: 92
Winter: 77
Tom: 74
Lily: 76
Maryia: 89
Who is the fourth? Andyer, this unlucky guy. Why is it begin () + 3 instead of + 4? I started to write this article.
I did not care about it. Later I found this problem with the reminder of ilovevc. Begin () is the first, begin () + 1
Is the second,... begin () + 3 is of course the fourth.

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.