stl_stack.h
stl_stack.h
// Filename: stl_stack.h // Comment By: 凝霜
// E-mail: mdl2009@vip.qq.com
// Blog: http://blog.csdn.net/mdl13412 ////////////////////////////////////////////////////////////////////////////////
// stack是一种先进后出(First In Last Out, FILO)的数据结构, 其只有一个出口
// 支持对栈顶元素的追加, 弹出, 获取, 但是不提供对其它位置元素的访问
////////////////////////////////////////////////////////////////////////////////
// 以下为使用deque时的布局
//
// 栈底 当前栈顶 预留的内存边界
// ↓ ↓ ↓
// --------------------------------------------------------------------
// | | | ...... | | | | | | | | ...... | | | X |
// --------------------------------------------------------------------
// ↑ ↑ ↑
// | | |
// | -------------------------------
// | 这里是尚未使用的预留内存, 可能为0
// |
// 仅支持在这里进行push(), pop(), top()操作
//////////////////////////////////////////////////////////////////////////////// /*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/ /* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/ #ifndef __SGI_STL_INTERNAL_STACK_H
#define __SGI_STL_INTERNAL_STACK_H __STL_BEGIN_NAMESPACE // 如果编译器不能根据前面模板参数推导出后面使用的默认参数类型,
// 那么就需要手工指定, 本实作stack内部容器默认使用deque
// 选用deque可以在存储空间不足时可以动态增加, 而且代价很低
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
template <class T, class Sequence>
#endif
class stack
{
// 特化的全局运算符, 提供operator==和<重载则构建出所有运算符
// 其具体细节见<stl_pair.h>中的说明
friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&); public:
// 由于stack仅支持对栈顶元素的操作, 所以不定义STL要求的
// pointer, iterator, difference_type
typedef typename Sequence::value_type value_type;
typedef typename Sequence::size_type size_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference; protected:
Sequence c; // 这个是我们实际维护的容器 public:
// 下面的操作完全使用内部容器的成员函数实现
// 这再次体现了STL高度的可复用性:-) // 判断stack是否为空
bool empty() const { return c.empty(); } // stack中元素个数
size_type size() const { return c.size(); } // 返回栈顶元素, 注意这里返回的是引用!!!
reference top() { return c.back(); }
const_reference top() const { return c.back(); } // 在栈顶追加新元素
void push(const value_type& x) { c.push_back(x); } // 移除栈顶元素, 注意不返回元素的引用,
// 很多初学者随机用此容器时经常误认为pop()操作同时会返回栈顶元素的引用
void pop() { c.pop_back(); }
}; // 判断两个stack是否相等, 就要测试其内部维护容器是否相等
// x.c == y.c会调用容器重载的operator ==
template <class T, class Sequence>
bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
return x.c == y.c;
} template <class T, class Sequence>
bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
return x.c < y.c;
} __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_STACK_H */ // Local Variables:
// mode:C++
// End:
stl_stack.h的更多相关文章
- 《STL源代码分析》---stl_stack.h读书笔记
Stack堆栈是频繁使用FILO数据结构,FILO指first in last out,最后出来. 因为只有一个堆叠端口,这也是在口腔进入口. 可以在堆栈中只能操作,你不能访问其它元件的堆叠.器. S ...
- STL源代码剖析 容器 stl_stack.h
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie stack ---------------------------------------- ...
- stl_queue.h
stl_queue.h // Filename: stl_queue.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http:/ ...
- STL源码剖析—stl_config
操作系统:centos 6.4STL源码版本:3.3 前言: 要看一个项目的源码,首先要选中切入点. 那么在sgi stl 标准库中,其切入点是什么呢? 答案是:stl_config ...
- C++ STL源码剖析
stl_config.h defalloc.h stl_alloc.h memory.cpp stl_construct.h stl_uninitialized.h stl_iterator.h ty ...
- std::map使用结构体自定义键值
使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...
- APUE中fcntl.h的使用及O_SYNC在Mac与Ubuntu下的测试
此部分测试涉及到APUE V3中,第三章的图3-12到图3-14. 通过fcntl.h提供的功能,修改fd的文件属性,本处增加O_SYNC功能,并测试其效果. 本文涉及代码: tree ch3 ch3 ...
- 关于apue.3e中apue.h的使用
关于apue.3e中apue.h的使用 近来要学一遍APUE第三版,并于此开博做为记录. 先下载源文件: # url: http://http//www.apuebook.com/code3e.htm ...
- YYModel 源码解读(二)之NSObject+YYModel.h (1)
本篇文章主要介绍 _YYModelPropertyMeta 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...
随机推荐
- 经常使用的eclipse快捷键-也适合sts
非常多内容来自互联网和已知文件,本人仅整理. 1. ctrl+shift+r:打开资源 这可能是全部快捷键组合中最省时间的了. 这组快捷键能够让你打开你的工作区中不论什么一个文件.而你仅仅须要按下文件 ...
- C#:ref和out的联系及区别。
总结以上四条得到ref和out使用时的区别是: ①:ref指定的参数在函数调用时候必须初始化,不能为空的引用.而out指定的参数在函数调用时候可以不初始化: ②:out指定的参数在进入函数时会清空自己 ...
- DICOM:C-GET服务
背景: 之前博文对照过多次C-MOVE与C-GET服务的差别,两者最大的差别在于C-GET是基于单个TCP连接的点对点的双方服务.而C-MOVE是基于两个TCP连接的三方服务(详情參见:<DIC ...
- Eclipse 中svn的合并与同步
Eclipse 中svn的合并与同步: 1. 从主干拉取到分支: 然后一直下一步,到完成就OK了. 2. 从分支代码合并到主干: 2.1.先将本地需要提交更新的代码提交更新到svn分支去 2.2. ...
- jquery 访问后台方法 并且获取后方法返回的数据
说明: 1.开发环境 asp.net MVC4 c#语言. 后台方法位于控制器中ProController.cs中 后台方法如下: public string GetNumber() { string ...
- iphone传感器
传感器 什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都是由传感 ...
- saltStack 证书管理
SaltStack 使用 SSL 签证的方式进行安全认证,我们可以在 Master 端看到 Minion 端的整数签证请求 1.查看当前证书签证情况 [root@SaltStack-Master ~] ...
- PAT 1054. 求平均值 (20)
本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位.当你计算平均值的时候, ...
- linux 创建账户
linux下创建用户 linux下创建用户(一) Linux 系统是一个多用户多任务的分时操作系统,不论什么一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. ...
- 读a paper of ICCV 2017 : Areas of Attention for Image Captioning
前言废话,作者说把代码公布在gitub上,但是迟迟没有公布,我发邮件询问代码情况,邮件也迟迟不回,表示很尴尬..虽然种种这些,但是工作还是好工作,这个没的黑,那我们今天就来详细的介绍这篇文章. 导论: ...