栈的使用
#include <iostream> #include <stack> using namespace std; int main() { stack<int> mystack; int sum (0); for (int i=1;i<=10;i++) mystack.push(i); cout<<"size:"<<mystack.size()<<endl;//一共有多少层 while (!mystack.empty()) { sum += mystack.top(); mystack.pop(); //cout<<"size:"<<mystack.size()<<endl; } cout << "total: " << sum << endl; system("pause"); return 0; }
step by step.