stack容器—C++笔记
栈不允许有遍历行为,只能访问栈顶,除非栈顶出栈才能看到后面的元素 #include<iostream>
using namespace std;
#include<stack>
//栈stack容器
void test(){
stack<int>s;
//入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
//只要栈不为空,查看栈顶,并且执行出栈操作
while(!s.empty()){
cout<<s.top()<<endl; //输出栈顶 //40 30 20 10
s.pop(); //出栈
}
cout<<s.size()<<endl; //0 为了看栈中元素全出栈了
}