stl_queue.h
stl_queue.h
// Filename: stl_queue.h // Comment By: 凝霜
// E-mail: mdl2009@vip.qq.com
// Blog: http://blog.csdn.net/mdl13412 ////////////////////////////////////////////////////////////////////////////////
// queue是一种先进先出(First In First Out, FIFO)的数据结构
// 它在前后有两个出口, 分别成为队头和队尾
// queue允许在队尾追加元素和访问队尾元素, 在队头获取和移除元素
// 除此之外其不支持其它元素的访问
////////////////////////////////////////////////////////////////////////////////
// 以下为使用deque时的布局
//
// 支持front()和pop() 支持back()和push()
// ↓ ↓
// 队头, 队尾
// ↓ ↓
// ---------------------------------------------------------------------
// | | | ...... | | | ...... | | | | ...... | | | X |
// ---------------------------------------------------------------------
// ↑ ↑ ↑ ↑
// | | | |
// ---------------------- -----------------------
// 这里是尚未使用的预留内存, 可能为0 这里是尚未使用的预留内存, 可能为0
//
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// priority_queue是一种允许用户以任何顺序将任何元素压入容器,
// 但是取出元素时一定是从最高优先级的元素开始取出
// 其默认使用的是vector作为容器, 默认的优先级比较使用less
// 其算法是使用binary-heap
////////////////////////////////////////////////////////////////////////////////
// 下面是其原理模型, 容器使用vector, 优先级比较决议用less
//
// 队尾, 支持push()
// ↓
// --------------------------------------------------------
// 支持pop()和top()--->| | | ...... | | | | | ...... | | | X |
// --------------------------------------------------------
// ↑ ↑ ↑
// | -----------------------
// 内部使用的是heap 这里是尚未使用的预留内存, 可能为0
////////////////////////////////////////////////////////////////////////////////
// 下面是容器使用vector, 优先级比较决议用less的情况下的一种实现技巧,
// 借用的是侯捷老师的例子, 本实作中使用的不是此技巧
// [A]
// |
// ---------------------------------
// | |
// [B] [C]
// | |
// ----------------------- -----------------------
// | | | |
// [D] [E] [F] [G]
// | |
// ----------- |
// | | |
// [H] [I] [J]
// vector中预留的内存, 了能为0
// ------------------
// ↓ ↓
// --------------------------------------------------------------------------
// | Not Use | A | B | C | D | E | F | G | H | I | J | | ...... | | end |
// --------------------------------------------------------------------------
//
// 具体算法请参看任意一本算法书, 如果没有, 扔了它:-)
//////////////////////////////////////////////////////////////////////////////// /*
*
* 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_QUEUE_H
#define __SGI_STL_INTERNAL_QUEUE_H __STL_BEGIN_NAMESPACE // 如果编译器不能根据前面模板参数推导出后面使用的默认参数类型,
// 那么就需要手工指定, 本实作queue内部容器默认使用deque
// 由于queue要求在队尾追加元素, 在队头获取和移除元素
// 所以非常适合使用deque
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
template <class T, class Sequence>
#endif
class queue
{
// 讲解见<stl_pair.h>中的运算符剖析
friend bool operator== __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);
friend bool operator< __STL_NULL_TMPL_ARGS (const queue& x, const queue& y); public:
// 由于queue仅支持对队头和队尾的操作, 所以不定义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 queue的标准接口, 都调用容器的成员函数进行实现
// 其接口和stack实现很接近, 参考<stl_stack.h>
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
reference front() { return c.front(); }
const_reference front() const { return c.front(); }
reference back() { return c.back(); }
const_reference back() const { return c.back(); }
void push(const value_type& x) { c.push_back(x); }
void pop() { c.pop_front(); }
}; // 详细讲解见<stl_pair.h>
template <class T, class Sequence>
bool operator==(const queue<T, Sequence>& x, const queue<T, Sequence>& y)
{
return x.c == y.c;
} template <class T, class Sequence>
bool operator<(const queue<T, Sequence>& x, const queue<T, Sequence>& y)
{
return x.c < y.c;
} #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = vector<T>,
class Compare = less<typename Sequence::value_type> >
#else
template <class T, class Sequence, class Compare>
#endif
class priority_queue
{
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; // 内部维护的容器
Compare comp; // 优先级决策判别式 public:
priority_queue() : c() {} // 用户可以指定自己的优先级决策函数
explicit priority_queue(const Compare& x) : c(), comp(x) {} // 使用[first, last)区间构造priority_queue
#ifdef __STL_MEMBER_TEMPLATES
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Compare& x)
: c(first, last), comp(x) { make_heap(c.begin(), c.end(), comp); }
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last)
: c(first, last) { make_heap(c.begin(), c.end(), comp); }
#else /* __STL_MEMBER_TEMPLATES */
priority_queue(const value_type* first, const value_type* last,
const Compare& x) : c(first, last), comp(x) {
make_heap(c.begin(), c.end(), comp);
}
priority_queue(const value_type* first, const value_type* last)
: c(first, last) { make_heap(c.begin(), c.end(), comp); }
#endif /* __STL_MEMBER_TEMPLATES */ // STL priority_queue标准接口
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); } // 返回优先级最高的元素
const_reference top() const { return c.front(); } // 插入元素, 并调整heap
void push(const value_type& x)
{
__STL_TRY {
c.push_back(x);
// 详细分析见<stl_heap.h>
push_heap(c.begin(), c.end(), comp);
}
__STL_UNWIND(c.clear());
} // 弹出优先级最高的元素
void pop() {
__STL_TRY {
// 详细分析见<stl_heap.h>
pop_heap(c.begin(), c.end(), comp);
c.pop_back();
}
__STL_UNWIND(c.clear());
}
}; // 不提供比较操作 __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_QUEUE_H */ // Local Variables:
// mode:C++
// End:
stl_queue.h的更多相关文章
- 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 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...
- YYModel 源码解读(一)之YYModel.h
#if __has_include(<YYModel/YYModel.h>) FOUNDATION_EXPORT double YYModelVersionNumber; FOUNDATI ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- afxcomctl32.h与afxcomctl32.inl报错
afxcomctl32.h与afxcomctl32.inl报错 编译公司一个几年前的老项目,是从VC6.0升级到VS2005的. 1.编译时报缺少头文件,于是附件包含目录,于是出现了以下报错: 1&g ...
- C标准头文件<math.h>
定义域错误可以理解为超出了函数的适用范围,如果发生了定义域错误,设errno为EDOM 如果结果不能表示为double值,则发生值域错误,如果结果上溢,则函数返回HUGE_VAL的值,设errno为E ...
随机推荐
- 2016/05/17 thinkphp3.2.2 分页的使用:①在Home下设置Publics文件夹或在thinkPHP下library的vender 把page.class.php 考贝进入 ②通过new 实例化方式调用 $page=new \Home\Publics\Page($total,3);
注意分页的方法有两种:一种是thinkphp3.2 自带的 另一种是之前新闻页用过的 显示效果稍有差别 显示效果: 细节问题: ①搜索页面 要加session判断 和 分页 ②修改 ...
- 【CodeM初赛B轮】A 贪心
[CodeM初赛B轮]A 题目大意:给你一棵树,起初所有点都是白色的,你每次都能选择一个白点i,将这个点i到根路径上的所有到i的距离<k[i]的点都染成黑色(根和i也算,已经被染成黑色的点还是黑 ...
- ASP.NET MVC + ADO.NET EF 项目实战(一):应用程序布局设计
什么叫上下文? 在你设计一个方法的时候,无法直接从方法参数或实例成员(字段或属性)获得的所有信息都是上下文.例如: 当前用户是谁? 刚才提供操作的数据库连接实例从哪里拿到? 这个方法从哪个 View ...
- oracle 如何完全删除干净
在安装oracle的时候如果出现了,指定的SID在本机上已经存在.这样的报错的话.这边你肯定是第二次在安装你的oracle了,这里出现这样的错误是你没有吧原先的那些关于oracle的东西给清理干净,这 ...
- mongo explain分析详解
1 为什么要执行explain,什么时候执行 explain的目的是将mongo的黑盒操作白盒化. 比如查询很慢的时候想知道原因. 2 explain的三种模式 2.1 queryPlanner 不会 ...
- Netty环境搭建 (源码死磕2)
[正文]netty源码 死磕2: 环境搭建 本小节目录 1. Netty为什么火得屌炸天? 1.1. Netty是什么? 1.2. Netty火到什么程度呢? 1.3. Netty为什么这么火? 2 ...
- 洛谷P3943 星空
洛谷P3943 星空 题目背景 命运偷走如果只留下结果, 时间偷走初衷只留下了苦衷. 你来过,然后你走后,只留下星空. 题目描述 逃不掉的那一天还是来了,小 F 看着夜空发呆. 天上空荡荡的,没有一颗 ...
- CSS3中transform,transition和animation的简单介绍和使用方法演示样例
transform是一个属性,本质跟width,height是一样的,加上transform也就是为类添加一个变换属性. transition是一个属性.它是用来控制过渡效果的,由于用transfor ...
- Java 迭代器
1.快速报错 public class FailFast { public static void main(String[] args) { List<String> c = new A ...
- Linux中rpm包管理器
包全名: 1.操作的包是没有安装的软件包时,使用全名,而且要注意路径 2.例如:jdk-8u131-linux-x64.rpm包名: 1.操作的是已经安装好的软件包,使用包名,是搜索/var/lib/ ...