stack类

1、stack类与queue类不同,stack是一种后进先出的容器适配器(类似杯子),它只允许在容器的末端进行插入和删除元素操作。其中,输出和输入元素的一端被称为栈顶

2、stack是利用deque,vector或list实现的适配器。默认情况下,stack通过deque实现

3、需要包含的命令

# include<stack>

using namespace std;

 

构造类函数

1、第一个函数是stack的默认构造函数,作用是构造一个空的堆栈------------------------stack<int> s;

2、第二个函数是stack的复制构造函数,作用是利用参数stack创建一个堆栈--------------stack<int> s2(s1);

3、第三个函数是stack运算符构造函数,作用是赋值---------------------------------------s2 = s1;

4、在更高版本的C++标准库中,stack的构造函数还有

# include<iostream>
# include<stack>
# include<deque>
# include<vector>
using namespace std;
int main()
{
vector<int> v(3,20);
deque<int> d(5,10);
stack<int> s1;
stack<int> s2(d);/////////////用deque初始化stack
stack<int,vector<int> > s3;////////////////要保留一个空格
stack<int,vector<int> > s4(v);///////////用vector初始化stack

cout<<s2.top()<<endl;
cout<<s4.top()<<endl;
return 0;
}

 

 

容量类函数--empty、size

 

存取类函数----top函数(返回栈顶元素)---通过调用顺序容器的back实现

 

操作类函数--push(添加)、pop(移除)

posted on 2017-01-12 11:42  qq77530202  阅读(241)  评论(0编辑  收藏  举报