栈


栈的主要操作
s.push(a); //将a压入栈顶(纯动作)
s.pop(); //删除栈顶的元素,但不会返回(纯动作)
s.top(); //返回栈顶的元素,但不会删除
s.size(); //返回栈中元素的个数
s.empty(); //检查栈是否为空,如果为空返回true,否则返回false
#include<bits/stdc++.h>
using namespace std;
stack<int> s;
int main(){
cout<<s.empty()<<endl;//1
cout<<s.size()<<endl;//0
s.push(1);s.push(3);s.push(5);s.push(7);
cout<<s.empty()<<endl;//0
cout<<s.size()<<endl;//4
cout<<"TOP:"<<s.top()<<endl;//7
s.pop();s.pop();
cout<<"TOP:"<<s.top()<<endl;//3
cout<<s.empty()<<endl;//0
cout<<s.size()<<endl;//2
return 0;
}

浙公网安备 33010602011771号