C++提供了stack的模板类,在使用时#include < stack>即可
stack模板的成员函数:s.push(value) 将一个对象入栈
s.pop() 删除栈顶元素,但不返回该元素值
s.top() 返回栈顶元素,但不将元素弹出栈
//
#include "stdafx.h"
#include <iostream>
#include <stack>
int _tmain(int argc, _TCHAR* argv[])
{
stack<int> intStack;
for (int i = 0; i < 10; i++)
{
intStack.push(i);//将0-9一次压栈
}
while (!intStack.empty())
{
cout << intStack.top() << endl;//访问栈顶的值
intStack.pop();//弹出栈顶的值
}
cin.get();
return 0;
}