1、栈
//栈
#include <iostream>
#include <stack>
using namespace std;
int main(void)
{
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(); //从栈顶移除 {1,2,3} -> {1,2}
printf("%d\n",s.top());//2
s.pop(); //{1,2} -> {1}
printf("%d\n",s.top());//1
s.pop(); //{1} -> {}
return 0;
}
2、队列
//队列
#include <iostream>
#include <queue>
using namespace std;
int main(void)
{
queue<int> q; //声明存储int类型数据的队列
q.push(1); //{} -> {1}
q.push(2); //{1} -> {1,2}
q.push(3); //{1,2} -> {1,2,3}
printf("%d\n",q.front());//1
q.pop(); //从队尾移除 {1,2,3} -> {2,3}
printf("%d\n",q.front());//2
q.pop(); //{2,3} -> {3}
printf("%d\n",q.front());//3
q.pop(); //{3} -> {}
return 0;
}