在c++中如何使用stl中的stack

/*----------------------------------------------------------*/ /*在c++中如何使用stl中的stack                               */

#include <iostream>
#include <stack>
using namespace std;

int main(){
stack<int> astack; //declare a object
int value;

cout <<"enter integer values, 's' to stop\n";

//while valid data, read value and add to stack
while (cin >> value) {
astack.push(value);
}

//Print values to standard output.
cout <<"Elements from the stack:\n";
while (!astack.empty()) {
cout<<astack.top()<<endl;  //access the top element.

astack.pop();   //remove top element from the stack.
}
return 0;
}

posted @ 2009-08-10 19:19  莫忆往西  阅读(196)  评论(0)    收藏  举报