c++ stl stack(FILO,容器配接器)
1.stack
stack是一种先进后出的数据结构,它只有一个出口。stack允许新增元素、移除元素、取得最顶元素。但除了最顶端外,没有任何其他方法可以存取stack的其他元素。换言之,stack不允许有遍历行为。
以某种既有容器作为底层结构,将其接口改变,使之符合“先进后出”的特性,形成一个stack,是很容易做到的。deque
是双向开口的数据结构,若以deque为底部结构并封闭其头端开口,便轻而易举地形成一个stack。因此,SGI STL便以deque作为缺省情况下的stack底部结构。
由于stack系以底部容器完成其所有工作,而具有这种“修改某物接口,形成另一种风貌”之性质者,称为adapter,因此,STL stack往往不被归类为container(容器),而被归类为container adapter。
template <class T,class Sequence = deque<T> >
class stack
{friend bool operator == __STL_NULL_TMPL_ARGS (const stack&,const stack&);friend bool operator < __STL_NULL_TMPL_ARGS (const stack&,const stack&);
public: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:bool empty() const { return c.empty(); }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); }void pop() { c.pop_back(); }
}
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;
}
stack所有元素的进出都必须符合“先进后出”的条件,只有stack顶端的元素,才有机会被外界取用。stack不提供走访功能,也不提供迭代器。
2.函数成员
/// The default constructor. Creates an empty stack.
stack();
/// The copy constructor.
stack(const stack&);
/// The assignment operator.
stack& operator=(const stack&);
/// Returns true if the stack contains no elements, and false otherwise.
bool empty() const;
/// Returns the number of elements contained in the stack.
size_type size() const;
/// Returns a mutable reference to the element at the top of the stack.
value_type& top();
/// Returns a const reference to the element at the top of the stack.
const value_type& top() const;
/// Inserts x at the top of the stack
void push(const value_type&);
/// Removes the element at the top of the stack.
void pop();
/// Compares two stacks for equality.
bool operator==(const stack&, const stack&);
/// Lexicographical ordering of two stacks.
bool operator<(const stack&, const stack&);
3.参考文献
本文内容摘录于《STL源码剖析》
c++ stl stack(FILO,容器配接器)相关推荐
- STL源码剖析 配接器
配接器(adapters)在 STL组件的灵活组合运用功能上,扮演着轴承.转换器的角色.Adapter这个概念,事实上是一种设计模式(design pattern). «Design Patterns ...
- STL源码剖析之配接器
adapter(配接器)在STL组件的灵活组合运用上,扮演者转换器的角色.adapter来源于一种适配器模式,其功能是:将一个class接口转换为另一个class的接口,使得原本因接口不兼容而不能合作 ...
- STL源码剖析 第八章 配接器
设计模式:将一个类的接口转化为另外一个类的接口 配接器的概观和分类 改变仿函数接口 函数配接器 :queue和stack 通过修饰deque函数接口来实现 改变容器接口 容器配接器 : ...
- 【C++ STL学习笔记】C++ STL序列式容器(array,vector,deque,list)
文章目录 C++ STL容器是什么? 迭代器是什么,C++ STL迭代器(iterator)用法详解 迭代器类别 迭代器的定义方式 C++序列式容器(STL序列式容器)是什么 容器中常见的函数成员 C ...
- STL——配接器(adapters)
一.配接器 <Design Patterns>一书提到23个最普及的设计模式,其中对adapter样式的定义如下:将一个class的接口转换为另一个class 的接口,使原本因接口不兼容而 ...
- 标准模板库(STL)之配接器(Adapter)
一般只有一个私有成员变量(某物)的类,且其全部的成员函数都是对该唯一的成员变量的存.取和修改(实现修改某物接口,形成另一种风貌,侯捷语),则该类即为对该私有成员变量的配接(adapter). Adap ...
- STL学习_配接器篇
STL学习_配接器篇 定义 配接器(Adapter)在STL组件的灵活组合运用功能上,扮演着轴承.转换器的角色.它事实上是一种设计模式.即将一个class的接口转换为另一个class的接口,使原本因接 ...
- C++ STL之stack栈容器
一.STL: 1)标准模版库,提供了通用的模版库和函数.如:向量.链表.队列.栈. 2)核心组建包括:容器(Containers).算法(Algorithms).迭代器(Iterators). 二.S ...
- STL stack 容器
STL stack 容器 Stack简介 stack是堆栈容器,是一种"先进后出"的容器. stack是简单地装饰deque容器而成为另外的一种容器. #i ...
最新文章
- mysql substr 中文乱码_刚碰到的MySql乱码的问题
- 如何显示jQuery中的加载微调器?
- struct.error: 'h' format requires -32768 number 32767
- UI自动化页面定位(一)
- 笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-08 数据修改
- 数据结构与算法之二叉搜索树
- MyBatis动态SQL_多表查询_延迟加载_缓存
- android 多态如何组件化,Android组件化之子模块之间通信方案
- 力扣209,长度最小的子数组(滑动窗口,JavaScript)
- 开源原生JavaScript插件-CJPCD(省市区联动)
- java mysql jdbc 封装_利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包括增删改查、JavaBean反射原理,附源代码)...
- 论文赏析[EMNLP19]用控制任务来设计并解释探测任务
- Spring Boot 2.x 自定义数据源 DruidDataSource(操作 mysql 数据库)
- 计算机编程语言的分类
- C11标准和C++11标准
- 5款App帮你创建时间轴
- 北京2020积分落户名单
- 艺术设计用计算机主板,学艺术设计的用什么电脑比较好
- Excel批量转置——录制会循环的宏
- Setuptools(Python打包工具)