C++ STL 容器之栈的使用

Stack 栈是种先进后出的容器,C++中使用STL容器Stack<T> 完美封装了栈的常用功能。

下面来个demo 学习下使用栈的使用。

 1 //引入IO流头文件
 2 #include<iostream>
 3 //引入栈头文件
 4 #include<stack>
 5 using namespace std;
 6 int main()
 7 {
 8     stack<int> st;
 9 
10     for (int i = 0; i < 10; i++) {
11         //将i压入栈中
12         st.push(i);
13     }
14     //遍历栈
15     while (!st.empty()) {
16         //打印栈顶元素
17         cout << st.top() << " ";
18         //弹出栈顶元素
19         st.pop();
20     }    
21     //换行
22     cout << endl;
23     //按任意键退出
24     cin.get();
25     return 0;
26 }

 执行结果:

分析图:

Tips: 栈的特点先入后出

原文地址:https://www.cnblogs.com/xingyunblog/p/7535251.html