头文件引用
#include<stack>
#include<string>
#include<iostream>
定义
stack<数据类型> 栈名;
数据类型小试了一下,目前只知道能用基本数据类型(之前设了个结构体试了,报错不是数据类型)
stack基本操作
s.empty() //栈为空,则返回true,反之返回false
s.push(a+b) //压栈操作
s.pop() //弹出栈操作
//使用方法
int a = s.top();
s.pop();
s.top() //返回栈顶的元素
s.size() //返回栈中的元素个数
//使用方法
int len = s.size;
在函数中引用栈,并对栈做出修改---》引用时写成
stack<类型>& 栈名
//example:
void jisuan(string str,int i,stack<int>& s1) {
int a = s1.top();
s1.pop();
int b = s1.top();
s1.pop();//出栈两个数字
switch (str[i]) {
case '+':s1.push(a + b); break;
case '-':s1.push(b - a); break;
case '*':s1.push(a * b); break;
case '/':s1.push(b / a); break;
}
}