在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;
}
#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;
}

浙公网安备 33010602011771号