STL容器适配器 stack, queue
stack是一种后进先出(last in first out)的数据结构。它只有一个出口,如图所示。stack允许新增元素,删除元素,取得最顶端元素。但除了最顶端外,没有其他任何地方可以存储stack的其他元素,换言之,stack不允许有遍历行为。
将元素推入stack的操作称为push, 将元素推出stack的操作称为pop.

为什么将stack称为适配器呢?我们先来看看适配器是怎么定义的。具有这种“修改某物接口,形成另一种风貌”之性质者,称为adapter(适配器)。换言之,由于stack的性质,可以使用vector或deque或list为底部结构进行一定的修改,轻易形成一个stack.
来看看stack的模板声明:
template<class T, class Sequence = deque<T>>
class stack {
// …
protected:
Sequeue c; //底层容器
public:
bool empty() const { return c.empty(); } size_type size() const { return c.size(); }
reference top() { return c.back(); }
const_reference top() { return c.back(); }
void push(const value_type& x) { c.push_back(x); }
void pop() { c.pop_back(); }
};
stack有2个模板参数,第二个Sequence(底层容器)默认的为deque。当然如果我们需要使用vector或list的话,必须自己指定出来。
测试代码:
#include <stack>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// stack<int, vector<int>> mystack; // 底层容器使用vector
// stack<int, deque<int>> mystack; // 底层容器使用deque (默认情况下)
stack<int, list<int>> mystack; // 底层容器使用list
mystack.push();
mystack.push();
mystack.push();
mystack.push();
mystack.push();
cout << mystack.size() << endl;
cout << mystack.top() << endl;
mystack.pop();
cout << mystack.top() << endl;
mystack.pop();
cout << mystack.top() << endl;
mystack.pop();
cout << mystack.top() << endl;
cout << mystack.size() << endl;
;
}
queue(队列)是一种先进先出(first in first out)的数据结构。他有两个端口,如下图所示。queue允许新增元素,删除元素,从最底端加入元素,取得最顶端元素等,但除了最底端可以加入,最顶端可以取出外,没有任何其他方法可以存取queue的其他元素。换言之,queue不允许有遍历行为。
将元素推入queue的操作称为push, 将元素推出queue的操作称为pop.

来看看queue的模板声明:
template<class T, class Sequence = deque<T>>
class queue {
// …
protected:
Sequeue c; //底层容器
public:
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
reference front() { return c.front(); }
const_reference front() { return c.front(); }
reference back() { return c.back(); }
const_reference back() { return c.back(); }
void push(const value_type& x) { c.push_back(x); }
void pop() {c.pop_front(); }
};
由于queue的结构性质,无法使用vector作为其底层容器(由于效率问题,vector类并未定义相关front函数),我们可以使用deque(默认)和list作为它的底层容器。
测试代码:
#include <queue>
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// queue<int, deque<int>> myque; // 使用deque作为底层容器(默认)
queue<int, list<int>> myque; // 使用list作为底层容器
myque.push();
myque.push();
myque.push();
myque.push();
myque.push();
cout << myque.size() << endl;
cout << myque.front() << endl;
myque.pop();
cout << myque.front() << endl;
myque.pop();
cout << myque.front() << endl;
myque.pop();
cout << myque.front() << endl;
cout << myque.size() << endl;
;
}
特别注意,由于stack,queue的特性,它们没有迭代器。
STL容器适配器 stack, queue的更多相关文章
- STL容器(Stack, Queue, List, Vector, Deque, Priority_Queue, Map, Pair, Set, Multiset, Multimap)
一.Stack(栈) 这个没啥好说的,就是后进先出的一个容器. 基本操作有: stack<int>q; q.push(); //入栈 q.pop(); //出栈 q.top(); //返回 ...
- STL之容器适配器stack的实现框架
说明:本文仅供学习交流,转载请标明出处,欢迎转载! 一提到适配器(adapter).我们就想到了早期用电话线上网所用的调制解调器,俗称"猫"."猫"的作用是实现 ...
- 容器适配器(一):queue
除了标准的顺序容器外,STL还提供了3种容器适配器,queue,priority_queue和stack 适配器是对顺序容器的包装,它的作用是简化接口. queue接口十分的简单,只有8个方法.再加上 ...
- c++ STL容器适配器
一.标准库顺序容器适配器的种类 标准库提供了三种顺序容器适配器:queue(FIFO队列).priority_queue(优先级队列).stack(栈) 二.什么是容器适配器 &q ...
- STL源码剖析——序列式容器#4 Stack & Queue
Stack stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口,元素的新增.删除.最顶端访问都在该出口进行,没有其他位置和方法可以存取stack的元素. ...
- 容器适配器————stack
只能访问 stack 顶部的元素:只有在移除 stack 顶部的元素后,才能访问下方的元素. 堆栈操作 top():返回一个栈顶元素的引用,类型为 T&.如果栈为空,返回值未定义. push( ...
- STL容器分析--stack
stack,顾名思义,表示栈,先进后出.
- 容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/d ...
- Stack&&Queue
特殊的容器:容器适配器 stack queue priority_queue:vector+堆算法---->优先级队列 stack: 1.栈的概念:特殊的线性结构,只允许 ...
随机推荐
- 【jq】c#零基础学习之路(5)自己编写简单的Mylist<T>
public class MyList<T> where T : IComparable { private T[] array; private int count; public My ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- java如何在eclipse编译时自动生成代码
用eclipse写java代码,自动编译时,如何能够触发一个动作,这个动作是生成本项目的代码,并且编译完成后,自动生成的代码也编译好了, java编辑器中就可以做到对新生成的代码的自动提示? 不生成代 ...
- $.map和$.extend来复制数组(OA差旅)
$.map:对数组的每个元素调用定义的回调函数并返回包含结果的数组. 对数组用指定的方法. //将原数组中每个元素加 4 转换为一个新数组. //jQuery 代码: $.map( [0,1,2], ...
- 043. asp.net主题之一初识主题和皮肤
主题由外观, 级联样式表(CSS), 图像和其它资源组成, 主题中至少包含外观. 他是在网站或Web服务器上的特殊目录中定义的: 外观: 外观文件是主题的核心内容, 用于定义页面中服务器控件的外观, ...
- 【转】libevent和基于libevent的网络编程
转自: http://www.cnblogs.com/nearmeng/p/4043548.html 1 libevent介绍和安装 介绍 libevent是一个轻量级的基于事件驱动的高性能的开源网络 ...
- lnmp平台菜鸟入门级笔记
LNMP平台搭建 Mysql安装 MySQL安装 回复收藏 分享 1 下载MySQL数据库l到/usr/local/src/[root@xin tmp]# cd ...
- S5Time BCD码存储
,时基为1秒. 当使用S5TIME时,定义数值的范围为0~999,而且要指明使用的时基.时基指定了时间单位. S5TIME时基及相应的时间范围如下: 时基 BCD码 时间范围 10 ms 00 10 ...
- RT-Thread下的串口驱动程序分析【转载】
编写本文稿的目的,在于通过分析stm32平台上的串口中断源码,学习 RTT中如何编写中断处理程序 如何编写RTT设备驱动接口代码 了解串行设备的常见处理机制 先以RTT官方源码中的STM32 BSP包 ...
- SQL Server 维护计划实现数据库备份(Step by Step)(转)
SQL Server 维护计划实现数据库备份(Step by Step) 一.前言 SQL Server 备份和还原全攻略,里面包括了通过SSMS操作还原各种备份文件的图形指导,SQL Server ...