stack 容器
1.1 stack基本概念
概念:stack是一种先进后出的数据结构,他只有一个出口。
栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历操作。
1.2 stack常用接口
#include<iostream> #include<stack> using namespace std; //栈stack容器 void test01(){ //特点:符合先进后出的数据结构 stack<int>s; //入栈 s.push(10); s.push(20); s.push(30); s.push(40); cout<<"栈的大小:"<<s.size()<<endl; //只要栈不为空,查看栈顶,并且执行出栈操作 while(!s.empty()){ //查看栈顶元素 cout<<"栈顶元素为:"<<s.top()<<endl; //40 30 20 10 //出栈 s.pop(); } cout<<"栈的大小:"<<s.size()<<endl; // return; } int main(){ test01(); system("pause"); return 0; }
总结: 入栈---push 出栈---pop 返回栈顶---top 判断是否为空---empty 返回栈大小---size