//stack基础操作
#include <bits/stdc++.h>
#include <iostream>
#include <stack>
using namespace std;
stack<int>myStack;
int main()
{
cout << "size of myStack:"<<myStack.size()<<endl;
for(int i=0;i<10;++i){
myStack.push(i); //把0、1、2、3、·····、9 push压入栈
}
cout<<"top of myStack:"<<myStack.top()<<endl;
cout<<"size of myStack:"<<myStack.size()<<endl;
int sum=0;
while(!myStack.empty()){
sum+=myStack.top(); //栈顶元素叠加,再pop出栈
myStack.pop();
}
cout<<sum<<endl;
if(myStack.empty()) cout<<"Is empty!"<<endl; //如果栈空
return 0;
}
/*
size of myStack:0
top of myStack:9
size of myStack:10
45
Is empty!
Process returned 0 (0x0) execution time : 3.242 s
Press any key to continue.
*/
熟悉训练之STL容器stack基础操作
最新推荐文章于 2023-06-04 19:27:09 发布