Stack && Queue

1. The stack
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 no container type is specified, the default container type is deque.
The sample code for defining the stack object is as follows:
stack<int> s1;
stack<string> s2;
The basic operations of stack are
: push to the stack, such as: s.push(x); Push an element to the top of the stack.
Pop the stack, such as: s.pop(); Note that the pop operation only deletes the top element of the stack, and does not return the element.
Access the top of the stack, for example: s.top(); Return the top element of the stack.
Judging the stack is empty, for example: s.empty(), when the stack is empty, return true.
Access the number of elements in the stack, for example: s.size().

2. The queue
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 and the other is the container
type . The element type is required, and the container type is optional, and the default is deque type.
Example code to define a queue object is as follows:
queue<int> q1;
queue<double> q2;

The basic operations of queue are:
enter the queue, such as: q.push(x); Connect x to the end of the queue.
Dequeue, for example: q.pop(); Pop the first element of the queue, note that the value of the popped element will not be returned.
Access the first element of the queue, such as: q.front(), which is the earliest element that was pushed into the queue.
Access the rear element, such as: q.back(), which is the last element pushed into the queue.
Judging that the queue is empty, for example: q.empty(), when the queue is empty, return true.

Access the number of elements in the queue, such as: q.size()

3. priority_queue
In the <queue> header file, another very 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 dequeued in the order of entry, but dequeued according to the priority order of the elements in the queue (the default is the larger one first, or you can specify your own priority order 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 latter two can be omitted. The default container is vector, and the default operator is less, that is, the small ones go forward and the large ones go backward (the elements at the end of the sequence are dequeued when the queue is dequeued).
Example code to define a priority_queue object is as follows:
priority_queue<int> q1;
priority_queue< pair<int, int> > q2; // Be sure to leave a space between the two angle brackets.
priority_queue<int, vector<int>, greater<int> > q3; //
The basic operation of defining a small first-out priority_queue is the same as queue.
When beginners use priority_queue, the most difficult thing may be how to define the comparison operator.
If it is a basic data type, or a class that has defined a comparison operator, you can directly use the less operator and greater operator of STL - the default is to use the less operator, that is, the smaller ones are queued first, and the larger ones are queued first.
If you want to define your own comparison operator, there are many ways, here is one of them: overloading the comparison operator. priority team列试图将两个元素x 和y 代入比较运算符(对less 算子,调用x<y,对greater 算子,调用x>y),若结果为真,则x 排在y 前面,y 将先于x 出队,反之,则将y 排在x 前面,x 将先出队。

看下面这个简单的示例:

#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; // 按照z 的顺序来决定t1 和t2 的顺序  
}  
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;  
}  
输出结果为(注意是按照z 的顺序从大到小出队的):  
3 3 6  
2 2 5  
1 5 4  
4 4 3  
再看一个按照z 的顺序从小到大出队的例子:
#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 t = q.top(); q.pop();  
cout << t.x << " " << t.y << " " << t.z << endl;  
}  
return 1;  
}  
输出结果为:  
4 4 3  
1 5 4  
2 2 5  
3 3 6
  1. STL中的stack、queue没有专门的clear()函数 
  2. 所以每次都要自己写清空stack、queue的函数  
  3.         while(!p.empty()){  
  4.             p.pop();  
  5.         }  
  6.         while(!q.empty()){  
  7.             q.pop();  
  8.         }  

ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10582    Accepted Submission(s): 5320


Problem Description
ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
 

Input
The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
 

Output
For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.
 

Sample Input
 
  
4 4 FIFO IN 1 IN 2 OUT OUT 4 FILO IN 1 IN 2 OUT OUT 5 FIFO IN 1 IN 2 OUT OUT OUT 5 FILO IN 1 IN 2 OUT IN 3 OUT
 

Sample Output
 
  
1 2 2 1 1 2 None 2 3
 
 
#include<iostream>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;
int main()
{
	int t;cin>>t;
	while(t--)
	{
		int n;
		string str;
		cin>>n>>str;
		if(str=="FIFO")
		{
			queue<int>q;
			for(int i=0;i<n;i++)
			{
				string cmd;
				cin>>cmd;
				if(cmd=="IN")
				{
					int m;
					cin>>m;
					q.push(m);//进队 
				}
				else if(cmd=="OUT")
				{
					if(!q.empty())//队列非空则拿出队首元素 
					{
						int m=q.front();
						q.pop();
						cout<<m<<endl;
					}
					else cout<<"None"<<endl;//队列为空 
				}
			}
		}
		else
		{
			stack<int>s;
			for(int i=0;i<n;i++)
			{
				string cmd;
				cin>>cmd;
				if(cmd=="IN")
				{
					int m;
					cin>>m;
					s.push(m);//进栈 
				}
				else if(cmd=="OUT")
				{
					if(!s.empty())//栈非空则拿出栈顶元素 
					{
						int m=s.top();
						s.pop();
						cout<<m<<endl;
					}
					else cout<<"None"<<endl;//栈为空 
				}
			}
		}
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325942652&siteId=291194637