// Filename:    stl_map.h// Comment By:  凝霜
// E-mail:      mdl2009@vip.qq.com
// Blog:        http://blog.csdn.net/mdl13412/*** 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_MAP_H
#define __SGI_STL_INTERNAL_MAP_H__STL_BEGIN_NAMESPACE#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#endif// 如果编译器不能根据前面模板参数推导出后面使用的默认参数类型,
// 那么就需要手工指定, 本实作map内部元素默认使用less进行比较, 其排序以key作为参照
// 内部维护的数据结构是红黑树, 具有非常优秀的最坏情况的时间复杂度
// 注意: map内元素的key不可一重复, 但是value允许重复
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
#else
template <class Key, class T, class Compare, class Alloc = alloc>
#endif
class map {
public:typedef Key key_type;                         // key类型typedef T data_type;                          // value类型typedef T mapped_type;typedef pair<const Key, T> value_type;        // 元素类型, 要保证key不被修改typedef Compare key_compare;                  // 用于key比较的函数// 关于为什么继承自binary_function见<stl_function.h>中的讲解// 被嵌套类提供key的比较操作class value_compare: public binary_function<value_type, value_type, bool>{friend class map<Key, T, Compare, Alloc>;protected :Compare comp;value_compare(Compare c) : comp(c) {}public:bool operator()(const value_type& x, const value_type& y) const {return comp(x.first, y.first);}};private:// 内部采用红黑树为数据结构, 其实现在<stl_tree.h>// 由于我剖析的版本没有侯捷老师的详细, 给出的是侯捷老师的版本typedef rb_tree<key_type, value_type,select1st<value_type>, key_compare, Alloc> rep_type;rep_type t;  // red-black tree representing mappublic:// 标记为'STL标准强制要求'的typedefs用于提供iterator_traits<I>支持// 注意: 迭代器, 引用类型都设计为const, 这是由map的性质决定的,//      如果用户自行更改其数值, 可能会导致内部的红黑树出现问题typedef typename rep_type::pointer pointer;                  // STL标准强制要求typedef typename rep_type::const_pointer const_pointer;typedef typename rep_type::reference reference;              // STL标准强制要求typedef typename rep_type::const_reference const_reference;typedef typename rep_type::iterator iterator;                // STL标准强制要求typedef typename rep_type::const_iterator const_iterator;typedef typename rep_type::reverse_iterator reverse_iterator;typedef typename rep_type::const_reverse_iterator const_reverse_iterator;typedef typename rep_type::size_type size_type;typedef typename rep_type::difference_type difference_type;  // STL标准强制要求map() : t(Compare()) {}explicit map(const Compare& comp) : t(comp) {}#ifdef __STL_MEMBER_TEMPLATEStemplate <class InputIterator>map(InputIterator first, InputIterator last): t(Compare()) { t.insert_unique(first, last); }template <class InputIterator>map(InputIterator first, InputIterator last, const Compare& comp): t(comp) { t.insert_unique(first, last); }
#elsemap(const value_type* first, const value_type* last): t(Compare()) { t.insert_unique(first, last); }map(const value_type* first, const value_type* last, const Compare& comp): t(comp) { t.insert_unique(first, last); }map(const_iterator first, const_iterator last): t(Compare()) { t.insert_unique(first, last); }map(const_iterator first, const_iterator last, const Compare& comp): t(comp) { t.insert_unique(first, last); }
#endif /* __STL_MEMBER_TEMPLATES */map(const map<Key, T, Compare, Alloc>& x) : t(x.t) {}map<Key, T, Compare, Alloc>& operator=(const map<Key, T, Compare, Alloc>& x){t = x.t;return *this;}// 返回用于key比较的函数key_compare key_comp() const { return t.key_comp(); }// 由于map的性质, value和key使用同一个比较函数, 实际上我们并不使用value比较函数value_compare value_comp() const { return value_compare(t.key_comp()); }iterator begin() { return t.begin(); }const_iterator begin() const { return t.begin(); }iterator end() { return t.end(); }const_iterator end() const { return t.end(); }reverse_iterator rbegin() { return t.rbegin(); }const_reverse_iterator rbegin() const { return t.rbegin(); }reverse_iterator rend() { return t.rend(); }const_reverse_iterator rend() const { return t.rend(); }bool empty() const { return t.empty(); }size_type size() const { return t.size(); }size_type max_size() const { return t.max_size(); }// 注意: 这里有一个常见的陷阱, 如果访问的key不存在, 会新建立一个T& operator[](const key_type& k){return (*((insert(value_type(k, T()))).first)).second;}// 返回的pair.second用于告知用户insert操作是否执行// 为true则表示真正进行插入, 为false则表示set中已存在待插入元素,// 不会重复插入void swap(map<Key, T, Compare, Alloc>& x) { t.swap(x.t); }// 对于相同的key, 只允许出现一次, bool标识pair<iterator,bool> insert(const value_type& x) { return t.insert_unique(x); }// 在position处插入元素, 但是position仅仅是个提示, 如果给出的位置不能进行插入,// STL会进行查找, 这会导致很差的效率iterator insert(iterator position, const value_type& x){return t.insert_unique(position, x);}#ifdef __STL_MEMBER_TEMPLATEStemplate <class InputIterator>void insert(InputIterator first, InputIterator last) {t.insert_unique(first, last);}
#elsevoid insert(const value_type* first, const value_type* last) {t.insert_unique(first, last);}void insert(const_iterator first, const_iterator last) {t.insert_unique(first, last);}
#endif /* __STL_MEMBER_TEMPLATES */// 擦除指定位置的元素, 会导致内部的红黑树重新排列void erase(iterator position) { t.erase(position); }// 会返回擦除元素的个数, 其实就是标识map内原来是否有指定的元素size_type erase(const key_type& x) { return t.erase(x); }void erase(iterator first, iterator last) { t.erase(first, last); }// 好吧, clear all, 再见吧红黑树void clear() { t.clear(); }// 查找指定key的元素iterator find(const key_type& x) { return t.find(x); }const_iterator find(const key_type& x) const { return t.find(x); }// 返回指定元素的个数, 其实就是测试元素是否在map中size_type count(const key_type& x) const { return t.count(x); }// 返回小于当前元素的第一个可插入的位置iterator lower_bound(const key_type& x) {return t.lower_bound(x); }const_iterator lower_bound(const key_type& x) const{return t.lower_bound(x);}// 返回大于当前元素的第一个可插入的位置iterator upper_bound(const key_type& x) {return t.upper_bound(x); }const_iterator upper_bound(const key_type& x) const{return t.upper_bound(x);}pair<iterator,iterator> equal_range(const key_type& x){return t.equal_range(x);}pair<const_iterator,const_iterator> equal_range(const key_type& x) const{return t.equal_range(x);}friend bool operator== __STL_NULL_TMPL_ARGS (const map&, const map&);friend bool operator< __STL_NULL_TMPL_ARGS (const map&, const map&);
};// 比较两个multiset比较的是其内部的红黑树, 会触发红黑树的operatortemplate <class Key, class T, class Compare, class Alloc>
inline bool operator==(const map<Key, T, Compare, Alloc>& x,const map<Key, T, Compare, Alloc>& y)
{return x.t == y.t;
}template <class Key, class T, class Compare, class Alloc>
inline bool operator<(const map<Key, T, Compare, Alloc>& x,const map<Key, T, Compare, Alloc>& y)
{return x.t < y.t;
}#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER// 如果编译器支持模板函数特化优先级
// 那么将全局的swap实现为使用map私有的swap以提高效率
template <class Key, class T, class Compare, class Alloc>
inline void swap(map<Key, T, Compare, Alloc>& x,map<Key, T, Compare, Alloc>& y)
{x.swap(y);
}#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1174
#endif__STL_END_NAMESPACE#endif /* __SGI_STL_INTERNAL_MAP_H */// Local Variables:
// mode:C++
// End:

《STL源码剖析》-- stl_map.h相关推荐

  1. 《STL源码剖析》学习-- 1.9-- 可能令你困惑的C++语法1

    最近在看侯捷的<STL源码剖析>,虽然感觉自己c++看得比较深一点,还是感觉还多东西不是那么明白,这里将一些细小的东西或者概念记录一下. 有些东西是根据<C++编程思想>理解的 ...

  2. STL源码剖析——P142关于list::sort函数

    在list容器中,由于容器自身组织数据的特殊性,所以list提供了自己的排序函数list::sort, 并且实现得相当巧妙,不过<STL源码剖析>的原文中,我有些许疑问,对于该排序算法,侯 ...

  3. STL源码剖析 stack 栈 概述->(使用deque双端队列 / list链表)作为stack的底层容器

    Stack是一种先进后出的数据结构,他只有一个出口 stack允许 新增元素.移除元素.取得最顶端的元素,但是无法获得stack的内部数据,因此satck没有遍历行为 Stack定义的完整列表 (双端 ...

  4. STL源码剖析 __type_traits

    traits编程 弥补了C++本身的不足 STL只对迭代器进行规范制定出了iterator_traits,SGI在此基础上进一步扩展,产生了__type_traits 双下划线的含义是这个是SGI内部 ...

  5. STL源码剖析 空间配置器 查漏补缺

    ptrdiff_t含义 减去两个指针的结果的带符号整数类型 ptrdiff_t (Type support) - C 中文开发手册 - 开发者手册 - 云+社区 - 腾讯云 std::set_new_ ...

  6. 《STL源码剖析》笔记——allocator

    六大组件间关系 部分STL文件包含关系 allocator包含于中: 实际实现于三个文件 : 1.stl_construct.h :对象的构造和析构 2.stl_alloc.h空间配置和释放 3.st ...

  7. STL源码剖析之配接器

    adapter(配接器)在STL组件的灵活组合运用上,扮演者转换器的角色.adapter来源于一种适配器模式,其功能是:将一个class接口转换为另一个class的接口,使得原本因接口不兼容而不能合作 ...

  8. 【STL源码剖析】list模拟实现 | 适配器实现反向迭代器【超详细的底层算法解释】

    今天博主继续带来STL源码剖析专栏的第三篇博客了! 今天带来list的模拟实现! 话不多说,直接进入我们今天的内容! 前言 那么这里博主先安利一下一些干货满满的专栏啦! 手撕数据结构https://b ...

  9. STL(C++标准库,体系结构及其内核分析)(STL源码剖析)(更新完毕)

    文章目录 介绍 Level 0:使用C++标准库 0 STL六大部件 0.1 六大部件之间的关系 0.2 复杂度 0.3 容器是前闭后开(左闭右开)区间 1 容器的结构与分类 1.1 使用容器Arra ...

  10. STL源码剖析学习七:stack和queue

    STL源码剖析学习七:stack和queue stack是一种先进后出的数据结构,只有一个出口. 允许新增.删除.获取最顶端的元素,没有任何办法可以存取其他元素,不允许有遍历行为. 缺省情况下用deq ...

最新文章

  1. Web前端开发人员和设计师必读文章推荐【系列九】
  2. 关于set sql_trace=ture 的一些用法
  3. 飞猪基于 Serverless 的云+端实践与思考
  4. VS2017创建Linux项目实现远程GDB调试
  5. linux系统根分区挂载出错 系统无法,用虚拟机安装linux时显示“没有定义根文件系统,请到分区菜单以修正此错误”,是什么意思,肿么弄谢谢各位...
  6. java int数组写入文件中_Java程序将int数组写入文件
  7. python协程框架_FastPy3.0 发布,高性能 python 协程web框架
  8. UVALive 6257 Chemist's vows --一道题的三种解法(模拟,DFS,DP)
  9. 用C语言实现:判断1000-2000年之间的闰年。
  10. 现代软件工程系列 学生精彩文章(7) 宝贵的教训
  11. 【下载源码】在线生成网页缩略图.超越Snap.com:WebSnap Beta 1.1 发布。感谢博客园的“萧寒”重写的底层。开源。...
  12. add p4 多个文件_绘图技巧01:继承特性创建对象之神器ADD
  13. 使用python-nmap 搭建基本端口扫描器
  14. ubuntu下配置php环境
  15. web开发必备的几个软件
  16. 通信教程 | CAN总线协议基础原理
  17. JFreechart中文在AIX下显示方框解决方法
  18. sails框架配置相关
  19. 安装部署Ceph Calamari
  20. urllib库学习 - 京东商城手机图片爬虫

热门文章

  1. Java学习指南从入门到入土
  2. 华为无线设备Mesh配置命令
  3. JAVA程序设计实用教程 第三章 习题3(2)
  4. OracleTechnologyNetwork
  5. 【浏览器】解决火狐和Chrome上不了网,只有IE能上网的问题
  6. 互联网项目发币一年,“简书们”活得好吗?
  7. 优雅地封装和使用 ViewBinding,该替代 Kotlin synthetic 和 ButterKnife 了
  8. Coursera-Neural Networks by Geoffrey Hinton
  9. Linux下进程状态转换,Linux进程状态转换图
  10. Vue+EleMentUI实现el-table-colum表格select下拉框可编辑