本节介绍一下STL中的stack
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
//本节学习STL-stack,同理定义方式和其他STL容器别无二致
printf("create a stack and push 1~5\n");
stack<int> st;
//push()用于将元素压栈,O(1)
for(int i=1;i<=5;i++)
st.push(i);
//top()可以返回栈顶元素,也可以直接访问,O(1)
int m=st.top();
printf("the top is %d m=st.top() m = %d\n",st.top(),m);
//pop()可以把栈顶元素弹出,O(1),返回值为void
for(int i=0;i<3;i++)
st.pop();
printf("After pop 3 times the top is %d\n",st.top());
//size()用来返回stack内的元素个数,O(1)
printf("Now the size of st is %d\n",st.size());
//empty()用于检测stack是否为空,为空返回true,否则返回false,O(1)
printf("create a empty stack st2\n");
stack<int> st2;
printf("so the st is ");
if(st.empty()==true)
printf("Empty\n");
else
printf("Not Empty\n");
printf("so the st2 is ");
if(st2.empty()==true)
printf("Empty\n");
else
printf("Not Empty\n");
return 0;
}