
正文
C++ STL 容器之栈的使用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示

Stack 栈是种先进后出的容器,C++中使用STL容器Stack<T> 完美封装了栈的常用功能。
下面来个demo 学习下使用栈的使用。
//引入IO流头文件
#include<iostream>
//引入栈头文件
#include<stack>
using namespace std;
int main()
{
stack<int> st; for (int i = ; i < ; i++) {
//将i压入栈中
st.push(i);
}
//遍历栈
while (!st.empty()) {
//打印栈顶元素
cout << st.top() << " ";
//弹出栈顶元素
st.pop();
}
//换行
cout << endl;
//按任意键退出
cin.get();
return ;
}
执行结果:

分析图:

Tips: 栈的特点先入后出






