// stack.cpp #include <iostream> #include <stack> #include <string> int main() { std::stack<int> ss; std::cout<<"size="<<ss.size()<<std::endl; for (int i=0; i<20; i++) ss.push(i); std::cout<<"size="<<ss.size()<<std::endl; while (!ss.empty()){ std::cout << ' ' << ss.top(); ss.pop(); //pop() has no return value, use top() to check value } std::cout<<std::endl; return 0; }
輸出:
martin@martin-K42Jc:~/Code$ g++ -o stack stack.cpp
martin@martin-K42Jc:~/Code$ ./stack
size=0
size=20
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
沒有留言:
張貼留言