STL 小白学习(5) stack栈

时间:2023-03-09 08:44:21
STL 小白学习(5) stack栈
#include <iostream>
#include <stack>
//stack 不遍历 不支持随机访问 必须pop出去 才能进行访问
using namespace std; void test01() {
//初始化
stack<int> s1;
stack<int> s2(s1);
//stack操作
//首先是压栈
s1.push();
s1.push();
s1.push();
//返回栈顶元素
cout << "栈顶元素为 " << s1.top() << endl;
//出栈
s1.pop(); //打印 不能遍历 cout top后只能pop掉 打印下一个元素
while (!s1.empty()) {
cout << s1.top() << endl;
s1.pop();
} //size()
cout <<"size: "<< s1.size() << endl; //栈容器存放对象指针 自练
//栈容器存放对象
}
int main() { }