3.2 栈与stack
1.头文件
#include<stack>
2.有关操作
stack<int/float/char...>a;//定义栈,任意数据类型
a.push(n);//将n放置于栈顶
a.top();//返回栈顶元素
a.pop();//弹出栈顶元素,一般与top()一起用
a.size();//返回栈的元素个数
a.empty();//检查栈是否为空,为空返回true,否则返回false
3.例题
hdu 1062
http://acm.hdu.edu.cn/showproblem.php?pid=1062
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
char ch;
scanf("%d",&n);
getchar();
while(n--)
{
stack<char>s;
while(1)
{
ch=getchar();
if(ch==' '||ch=='\n'||ch==EOF)
{
while(!s.empty())
{
cout<<s.top();
s.pop();
}
if(ch=='\n'||ch==EOF) break;
cout<<" ";
}
else s.push(ch);
}
cout<<endl;
}
return 0;
}