1.stack是C++中的容器适配器,被设计为先进后出(FILO),元素的插入和删除只能在尾部进行
2.stack支持以下操作:
- empty(判断是否为空,返回true为空,返回false为非空)
- size(返回栈的元素个数)
- top(弹出栈顶元素)
- push(入栈)
- pop(出栈,弹出栈顶元素)
#include<utility>
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
using namespace std;
int main()
{
stack<int> st;
if (st.empty())
{
cout << "空0" << endl;
}
for (int i = 1; i <= 5; i++)
{
st.push(i); //元素入栈
}
cout << "栈中元素个数:" << st.size() << endl;
if (st.empty())
{
cout << "空1" << endl;
}
for (int i = 1; i <= 5; i++)
{
cout << st.top() << endl;//打印栈顶元素
st.pop(); //出栈
}
cout << "栈中元素个数:" << st.size() << endl;
if (st.empty())
{
cout << "空2" << endl;
}
system("pause");
return 0;
}