#include<stdio.h>
#include<stack>//栈标准库
//利用STL库进行栈的基本操作
using namespace std;
//使用存储空间
int main()
{
stack<int>S;//生成栈S;
int i;
int n=5;
for(i=0;i<5;i++)
S.push(i);//栈的压入操作;
int size;
size=S.size();
printf("%d\n",size);//计算栈的大小(元素个数);
int top;
top=S.top();//此时将栈顶元素返回,并不抛出,返回值为所定义类型;
printf("%d\n",top);
S.pop();//抛出栈顶元素;
int top2;
top2=S.top();//此时将栈顶元素返回,返回值为所定义类型;
printf("%d\n",top2);
while(!S.empty())//进行判空操作,如果为空返回1,不空返回0;
{
printf("%d ",S.top());
S.pop();
}
return 0;
}
STL库中,栈的应用
最新推荐文章于 2024-04-14 22:04:26 发布