栈&&队列

1、stack
stack 模板类的定义在<stack>头文件中。
stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要
的,在不指定容器类型时,默认的容器类型为deque。
定义stack 对象的示例代码如下:
stack<int> s1;
stack<string> s2;
stack 的基本操作有:
入栈,如例:s.push(x); 栈顶压入一元素。
出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶,如例:s.top(); 返回栈顶元素。
判断栈空,如例:s.empty(),当栈空时,返回true。
访问栈中的元素个数,如例:s.size()。

2、queue
queue 模板类的定义在<queue>头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。

访问队列中的元素个数,如例:q.size()

3、priority_queue
在<queue>头文件中,还定义了另一个非常有用的模板类priority_queue(优先队列)。优先队列与队列的差别在于优先队列不是按照入队的顺序出队,而是按照队列中元素的优先权顺序出队(默认为大者优先,也可以通过指定算子来指定自己的优先顺序)。
priority_queue 模板类有三个模板参数,第一个是元素类型,第二个容器类型,第三个是比较算子。其中后两个都可以省略,默认容器为vector,默认算子为less,即小的往前排,大的往后排(出队时序列尾的元素出队)。
定义priority_queue 对象的示例代码如下:
priority_queue<int> q1;
priority_queue< pair<int, int> > q2; // 注意在两个尖括号之间一定要留空格。
priority_queue<int, vector<int>, greater<int> > q3; // 定义小的先出队
priority_queue 的基本操作与queue 相同。
初学者在使用priority_queue 时,最困难的可能就是如何定义比较算子了。
如果是基本数据类型,或已定义了比较运算符的类,可以直接用STL 的less 算子和greater算子——默认为使用less 算子,即小的往前排,大的先出队。
如果要定义自己的比较算子,方法有多种,这里介绍其中的一种:重载比较运算符。优先队列试图将两个元素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;
}


猜你喜欢

转载自blog.csdn.net/luojiushenzi/article/details/80171769