栈是先进后出的数据结构
1.头文件#include <stack>
2.声明 stack<int,char,string等数据类型> 变量名;
3。基本函数。(1)empty看栈是否为空。
stack<int> mystack;
!mystack.empty() 空返回true;非空返回false
(2)push 在栈顶插入数据;
stack<int> mystack;
mystack.push(10);
mystack.push(20);
(3)pop 删除栈顶数据
mystack.pop();
(4)top 取栈顶数据
mystack.push(10);
mystack.push(20);
mystack.top() -= 5;
cout << "mystack.top() is now " << mystack.top() << endl;
输出的是15.
(5)size 返回栈的元素个数
stack<int> myints;
cout << "0. size: " << (int) myints.size() << endl;
for (int i=0; i<5; i++) myints.push(i);
cout << "1. size: " << (int) myints.size() << endl;
输出0和5。
4.清空栈算法。while(!mystack.empty()) mystack.pop();