您的位置:首页 > 其它

STL栈与队列的基础用法

2014-02-15 21:15 218 查看
纯抄书,备忘。

栈:

#include<stack>
#include<cstdio>
using namespace std;
int main()
{
stack<int> s;//声明存储int类型数据的栈
s.push(1);//{}->{1}
s.push(2);//{1}->{1,2}
s.push(3);//{1,2}->{1,2,3}
printf("%d\n",s.top());//3
s.pop();//从栈顶移除
printf("%d\n",s.top());
s.pop();
printf("%d\n",s.top());
s.pop();
return 0;
}
队列:

#include<queue>
#include<cstdio>
using namespace std;
int main()
{
queue<int>que;
que.push(1);
que.push(2);
que.push(3);
printf("%d\n",que.front());//1
que.pop();//从队尾移除,{1,2,3}->{2,3}
printf("%d\n",que.front());
que.pop();
printf("%d\n",que.front());
que.pop();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: