Stack and queue are also frequently used data containers in programming. STL provides us with a convenient Implementation of the stack queue.
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 ()
The following is a program that uses string and stack to Solve the Problem 1064 -- parencoding.
# Include <iostream>
# Include <string>
# Include <stack>
Using namespace STD;
Main ()
{
Int N;
Cin> N;
For (INT I = 0; I <n; I ++)
{
Int m;
Cin> m;
String STR;
Int leftpa = 0;
For (Int J = 0; j <m; j ++) // read P encoding to construct a string of parentheses
{
Int P;
Cin> P;
For (int K = 0; k <p-leftpa; k ++) STR + = '(';
STR + = ')';
Leftpa = P;
}
Stack <int> S;
For (string: iterator it = Str. Begin (); it! = Str. End (); It ++)
{// Construct M Encoding
If (* It = '(')
S. Push (1 );
Else
{
Int P = S. Top (); S. Pop ();
Cout <p <"";
If (! S. Empty () S. Top () + = P;
}
}
Cout <Endl;
}
Return 1;
}
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 is the element type, the other is the container type, and the element type is necessary. The container type is optional. The default is the 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 ). The difference between the priority queue and the queue is that the priority queue is not in the order of queuing, but in the priority order of the elements in the queue (by default, the priority queue is greater than the queue, you can also 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 comparison operator. The first two can be omitted. The default container is vector, and the default operator is less. That is, small rows are arranged in the front row and large rows are arranged in the back row (when the elements at the end of the sequence are queued ).
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 that has already defined a comparison operator, you can directly use the STL less operator and greater operator-the default is to use the less operator, that is, small to the front row, A large team 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. The priority queue 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, then, X is in front of Y, Y is in front of X, and X is in front of X.
Let's look at the following 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 & T1, const T & T2)
{
Return t1.z <t2.z; // The Order of T1 and T2 is determined by the Z sequence.
}
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 = Q. Top (); q. Pop ();
Cout <t. x <"" <t. Y <"" <t. z <Endl;
}
Return 1;
}
The output result is (from large to small in Z order ):
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 & 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 = Q. Top (); q. Pop ();
Cout <t. x <"" <t. Y <"" <t. z <Endl;
}
Return 1;
}
Output result:
4 4 3
1 5 4
2 2 5
3 3 6
If we reload the comparison operator in the first example as follows:
Bool operator <(const T & T1, const T & T2)
{
Return t1.z> t2.z; // The Order of T1 and T2 is determined by Z.
}
The program in the first example will get the same output result as the program in the second example.
Let's review the code of question 1067 -- uugly numbers implemented with priority queue:
# Include <iostream>
# Include <queue>
Using namespace STD;
Typedef pair <unsigned long int, int> node_type;
Main (INT argc, char * argv [])
{
Unsigned long int result [1500];
Priority_queue <node_type, vector <node_type>, greater <node_type> q;
Q. Push (make_pair (1, 3 ));
For (INT I = 0; I <1500; I ++)
{
Node_type node = Q. Top ();
Q. Pop ();
Switch (node. Second)
{
Case 3: Q. Push (make_pair (node. First * 2, 3 ));
Case 2: Q. Push (make_pair (node. First * 3, 2 ));
Case 1: Q. Push (make_pair (node. First * 5, 1 ));
}
Result [I] = node. first;
}
Int N;
Cin> N;
While (n> 0)
{
Cout <result [n-1] <Endl;
Cin> N;
}
Return 1;
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/camel_flying/archive/2009/08/17/4454192.aspx