First.栈(Stack)
定义:后进先出的线性表
操作:
#include<stack> 头文件
stack<int> s; 创建int类型的栈s
s.push(x); 将x放入栈中
s.top(); 读取栈顶元素
s.pop(); 释放栈顶
s.size(); 返回栈中元素数目
s.empty(); 若栈为空则返回真,反之则返回假
示范代码:
1 #include<iostream> 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<algorithm> 5 #include<map> 6 #include<stack> 7 #include<queue> 8 9 using namespace std; 10 11 int main() 12 { 13 stack<int> s1; 14 int x=3; 15 s1.push(x); 16 cout<<"x="<<s1.top()<<endl; 17 int y; 18 y=s1.top(); 19 cout<<"y="<<y<<endl; 20 cout<<"size: "<<s1.size()<<endl; 21 cout<<"empty? "<<s1.empty()<<endl; 22 s1.pop(); 23 cout<<"size: "<<s1.size()<<endl; 24 cout<<"empty? "<<s1.empty()<<endl; 25 return 0; 26 }