int main(){
//定义一个空栈a
stack<int> a;
for(int i = 1; i <= 3; i++)
a.push(i);
cout << "size of a is " << a.size()<< endl;
cout << "element of a is: " ;
for(; a.size() > 0;){
// cout << a.pop() << " "; pop直接删除元素并没有返回,所以这种用法是错误的
cout << a.top() << " ";
a.pop();
}
cout <<endl;
cout << "after pop all element in a,the size = " << a.size()<< endl;
cout << endl;
return 0;
}
输出结果如下:
原文:https://www.cnblogs.com/zuixime0515/p/10514158.html