一.stack(栈)
常见的操作:
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> S;
S.push(3);
S.push(10);
S.push(100);
cout<<S.size()<<endl;
S.pop();
cout<<S.top()<<endl;;
if(!S.empty())
cout<<"yes"<<endl;
return 0;
}
逆波兰表示法的编程实现:
#include <bits/stdc++.h>
using namespace std;
//积累两个重要的函数:
//str.c_str();
//atoi()
int main(){
stack<int> S;
int a, b;
string str;
while(cin>>str){
if (str[0] == '+'){
a = S.top();
S.pop();
b = S.top();
S.pop();
//cout<<a+b<<endl;
S.push(a+b);
}
else if (str[0] == '-'){
a = S.top();
S.pop();
b = S.top();
S.pop();
//cout<<a-b<<endl;
S.push(b-a);//注意减数与被减数的位置!!!
}
else if (str[0] == '*'){
a = S.top();
S.pop();
b = S.to