转载:http://blog.csdn.net/morewindows/article/details/6950881

栈(statck)这种数据结构在计算机中是相当出名的。栈中的数据是先进后出的(First In Last Out, FILO)。栈只有一个出口,允许新增元素(只能在栈顶上增加)、移出元素(只能移出栈顶元素)、取得栈顶元素等操作。在STL中,栈是以别的容器作为底部结构,再将接口改变,使之符合栈的特性就可以了。因此实现非常的方便。下面就给出栈的函数列表和VS2008中栈的源代码,在STL中栈一共就5个常用操作函数(top()、push()、pop()、 size()、empty() ),很好记的。

VS2008中栈的源代码

友情提示:初次阅读时请注意其实现思想,不要在细节上浪费过多的时间。

//VS2008中 stack的定义
template<class _Ty, class _Container = deque<_Ty> >
class stack
{ // LIFO queue implemented with a container
public:
typedef _Container container_type;
typedef typename _Container::value_type value_type;
typedef typename _Container::size_type size_type;
typedef typename _Container::reference reference;
typedef typename _Container::const_reference const_reference; stack() : c()
{ // construct with empty container
} explicit stack(const _Container& _Cont) : c(_Cont)
{ // construct by copying specified container
} bool empty() const
{ // test if stack is empty
return (c.empty());
} size_type size() const
{ // test length of stack
return (c.size());
} reference top()
{ // return last element of mutable stack
return (c.back());
} const_reference top() const
{ // return last element of nonmutable stack
return (c.back());
} void push(const value_type& _Val)
{ // insert element at end
c.push_back(_Val);
} void pop()
{ // erase last element
c.pop_back();
} const _Container& _Get_container() const
{ // get reference to container
return (c);
} protected:
_Container c; // the underlying container
};

可以看出,由于栈只是进一步封装别的数据结构,并提供自己的接口,所以代码非常简洁,如果不指定容器,默认是用deque来作为其底层数据结构的。下面给出栈的使用范例:

//栈 stack支持 empty() size() top() push() pop()
#include <iostream>
#include <stack>
#include <vector>
#include <list>
#include <cstdio>
using namespace std;
int main()
{
//可以使用list或vector作为栈的容器,默认是使用deque的。
stack<int, list<int> > a;
stack<int, vector<int> > b;
int i; //压入数据
for (i = ; i < ; i++)
{
a.push(i);
b.push(i);
} //栈的大小
cout>>a.size()>>' '>>b.size()>>endl; //取栈项数据并将数据弹出栈
while (!a.empty())
{
cout>>a.top()>>' ';
a.pop();
}
cout>>endl; while (!b.empty())
{
cout>>b.top()>>' ';
b.pop();
}
cout>>endl;
return ;
}

做个总结:

1、C++中stl的stack(栈)以其他容器(deque默认,vector,list)作为底层数据结构而形成,只是修改了接口以满足栈的特性;

2、stack(栈)的常用操作:top(),push(elem),pop(),empty(),size();

3、用法:

stack<int, list<int> >      a;

stack<int, vector<int> >   b;

stack<int>   c;

最新文章

  1. AndroidStudio不能解析R的一种可能
  2. JQuery 遍历子元素+ each函数的跳出+提取字符串中的数字
  3. 【转】各版本IIS下ASP.net请求处理过程区别
  4. Debian自带浏览器IceWeasel的中文化
  5. git for windows+TortoiseGit客户端的使用
  6. 关于C#基类和子类函数调用问题
  7. hdu 1599 find the mincost route(flyod求最小环)
  8. js管理内存
  9. 不可不知的 Android strings.xml 那些事
  10. Unity GameObject Class
  11. java代码中init method和destroy method的三种使用方式
  12. 边做边学入门微信小程序之仿豆瓣评分
  13. Spring Batch 专题
  14. &lt;TCP/IP原理&gt; (二) OSI模型和TCP/IP协议族
  15. Kotlin入门(31)JSON字符串的解析
  16. maven 常用备忘录
  17. 借助ssh隧道和中间主机,使本地主机可以直连远程主机
  18. MapGIS数据中心设计器 帮助文档
  19. 腾讯云主机的公网无法访问,putty和FileZilla连接不上
  20. P2799国王的魔镜

热门文章

  1. 另类安装系统——PE工具提取
  2. 项目中重新引用WCF报错
  3. 实现百度外卖APP个人中心头像&quot;浪&quot;起来的动画效果
  4. 《Cocos2d-x实战 工具卷》上线了
  5. 4月12日学习笔记——jQuery操作属性和样式
  6. 【风马一族_Python】 安装pip与Numpy
  7. Linux磁盘与文件系统概念理解
  8. 使用tortoisegit管理git 和 权限验证
  9. SQL server数据库内置账户SA登录设置
  10. JavaScript 同源策略