[ZZ] C + + Stack/queue

Source: Internet
Author: User

How to use Stl--stack/queue
2010-01-05 17:36
Stack (stack) and queue (queue) are also frequently used in the design of data containers, STL provides us with a convenient stack (stack) queue (queue) implementation.


To be exact, the stack and queue in the STL are different from those of vectors, lists, and so on, instead of repackaging these containers. Here we do not go into the details of the STL stack and queue implementations, but to understand some of their basic uses.


1. Stack


The stack template class is defined in the <stack> header file.


The stack template class requires two template parameters, one element type, one container type, but only the element type is necessary, and the default container type is deque when no container type is specified.


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


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


The basic operations of stack are:


Into the stack, as in example: S.push (x);


Out of the stack, as in example: S.pop (); Note that the stack operation simply deletes the top element of the stack and does not return the element.


Access to the top of the stack, as in example: S.top ()


Determine the stack null, as an example: S.empty (), returns True when the stack is empty.


Access the number of elements in the stack, such as: 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 requires two template parameters, one element type, one container type, the element type is necessary, the container type is optional, and the default is the Deque 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:


Queue, as in example: Q.push (x); Connect X to the end of the queue.


Out of the team, as in Example: Q.pop (); The first element of the popup queue, note that the value of the popup element is not returned.


Access the first element of the team, such as: Q.front (), which is the first element to be pressed into the queue.


Access the tail element, such as: Q.back (), which is the element that was last pressed into the queue.


Determine if the queue is empty, as in example: Q.empty (), returns True when the queue is empty.


Access to the number of elements in the queue, as in example: Q.size ()


3, Priority_queue


Another very useful template class, Priority_queue (priority queue), is also defined in the <queue> header file. The difference between the priority queue and the queue is that the priority queue is not out of the queue in the queued order, but rather in the order of precedence of the elements in the queues (the default is large, or you can specify your own precedence by assigning operators).


The Priority_queue template class has three template parameters, the first one is the element type, the second container type, and the third is the comparison operator. 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; Note Be sure to leave a blank between the two angle brackets.
Priority_queue<int, Vector<int&gt, greater<int> > Q3; Define a small first-out team


The basic operation of the priority_queue is 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:


#include <iostream>
#include <queue>
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 &AMP;T1, const T &AMP;T2)
{
return T1.z < t2.z; Determine the order of T1 and T2 in order of Z
}
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 ())
{
T t = q.top (); Q.pop ();
cout << t.x << "<< t.y <<" << t.z << Endl;
}
return 1;
}


The output is (note that the queue is from large to small in order of Z):


3 3 6
2 2 5
1 5 4
4 4 3


Let's look at an example from small to large teams in Z Order:


#include <iostream>
#include <queue>
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 &AMP;T2)
{
return t1.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 ())
{
T t = q.top (); Q.pop ();
cout << t.x << "<< t.y <<" << t.z << Endl;
}
return 1;
}


The output is:


4 4 3
1 5 4
2 2 5
3 3 6


If we overload the comparison operator in the first example to:


BOOL operator < (const T &AMP;T1, const T &AMP;T2)
{
return t1.z > t2.z; Determine the order of T1 and T2 in order of Z
}


The first example of the program will get the same output as the program of the second example.


1.6 Nth_element specifying element ordering
Nth_element a sort that is easy to read but explains rather troublesome. It is more convenient to use examples:
There are 10 students in the class, and I want to know the students who are ranked in the bottom 4th place.
If you want to meet the above requirements, you can use sort order, and then take the 4th bit (because it is from the small to the big row), the more intelligent friends will use Partial_sort, only the top 4, and then get 4th place. In fact this is you or waste, because the first two bits you do not need to sort at all, at this time, you need nth_element:
Template <class randomaccessiterator>
void Nth_element (randomaccessiterator first, randomaccessiterator nth,
Randomaccessiterator last);


Template <class Randomaccessiterator, class strictweakordering>
void Nth_element (randomaccessiterator first, randomaccessiterator nth,
Randomaccessiterator last, strictweakordering comp);
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> ());
To be replaced by:
Nth_element (Vect.begin (), Vect.begin () +3, Vect.end (),less<student> ());
The result of the operation is:
------before sort ...
tom:74
jimy:56
mary:92
Jessy:85
jone:56
bush:52
Winter:77
andyer:63
lily:76
maryia:89
-----after sort ....
jone:56
bush:52
jimy:56
andyer:63
Jessy:85
mary:92
Winter:77
tom:74
lily:76
maryia:89
Who's the fourth one? Andyer, this unlucky fellow. Why is begin () +3 instead of +4? I did not care when I began to write this article, and later, in ILOVEVC's reminder, I found this problem. Begin () is the first, begin () +1 is the second, ... begin () +3 of course it's the fourth one.

[ZZ] C + + Stack/queue

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.