stack是栈,可以有的操作包括入栈push()、出栈pop()、获得栈顶元素top()、判空empty(),获得空间元素个数等
// stack函数的基本操作
#include<stdio.h>
#include<iostream>
#include<stack>
#include<list>
#include<vector>
#include<string>
using namespace std;
int main()
{
stack<int,vector<int> > s;
for(int i=0;i<10;i++)
{
s.push(i);//stack的push压栈操作
}
cout<<"stack的大小为:"<<s.size()<<endl; //size函数
cout<<"stack的top操作"<<s.top()<<endl;
while(!s.empty())
{
cout<<s.top()<<"\t";
s.pop();
}
stack<string,list<float> > s1;
for(int i=0;i<10;i++)
{
s1.push(i+0.1);//stack的push压栈操作
}
cout<<"stack的大小为:"<<s1.size()<<endl; //size函数
cout<<"stack的top操作"<<s1.top()<<endl;
while(!s1.empty())
{
cout<<s1.top()<<"\t";
s1.pop();
}
system("pause");
}
本文详细介绍了栈的数据结构及其基本操作,包括入栈、出栈、获取栈顶元素、判断是否为空和获取元素个数,并通过实例展示了如何使用C++中的stack容器实现这些操作。
543

被折叠的 条评论
为什么被折叠?



