1.基本概念
stack是一种先进后出的数据结构,只有一个出口
2.常用接口
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<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;
//出栈
s.pop();
}
cout << "栈的大小为: " << s.size() << endl;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
总结:
入栈--push
出栈--pop
返回栈顶--top
判断是否为空--empty
返回栈大小--size