STL源码剖析——map

map是STL中的标准关系容器,它存储的元素时pair,拥有键值key和实值value。按照键值key存储到红黑树中。map中不允许两个键值相同的元素。当元素插入到红黑树中后,键值key不可以再修改,但是其实值value可以修改,因为map的迭代器不是constant iterator,而是mutable iterator。

<span style="font-family:Microsoft YaHei;font-size:18px;">G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_map.h 完整列表
/*
 *
 * 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

#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
//Key是键值key类型,T是实值data类型
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:

// typedefs:

  typedef Key key_type;	// 键值类型
  typedef T data_type;		// 实值,数据类型
  typedef T mapped_type;	// 
  typedef pair<const Key, T> value_type;	// 元素类型(键值/真值)
  typedef Compare key_compare;	// 键值的比较函数
    
  // 下面定义一个functo,其作用就是调用元素比较函数。
  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:

  //以map类型(一个pair)的第一个类型作为TB-tree的键值类型.
  //所以在RB-tree中,键值key不能修改,但是数值data可以修改。
  typedef rb_tree<key_type, value_type, 
                  select1st<value_type>, key_compare, Alloc> rep_type;
  rep_type t;  // 红黑树作为底层数据结构
public:
  typedef typename rep_type::pointer pointer;
  typedef typename rep_type::const_pointer const_pointer;
  typedef typename rep_type::reference reference;
  typedef typename rep_type::const_reference const_reference;
  //没有定义为 RB-tree的 const_iterator。我想应该是虽然键值key不能修改,
  //但是数值data可以修改。
  typedef typename rep_type::iterator iterator;
   
  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;

  // allocation/deallocation
  // 注意, map 一定使用 insert_unique() 而不使用 insert_equal()。
  // multimap 才使用 insert_equal()。

  map() : t(Compare()) {}
  explicit map(const Compare& comp) : t(comp) {}

#ifdef __STL_MEMBER_TEMPLATES
  template <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); }
#else
  map(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; 
  }

  // accessors:
  // 以下所有的 map操作行為,RB-tree 都已提供,所以map只要调用即可。

  key_compare key_comp() const { return t.key_comp(); }
  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(); }
  //重载[],返回的是引用
  T& operator[](const key_type& k) {
    return (*((insert(value_type(k, T()))).first)).second;
  }
  void swap(map<Key, T, Compare, Alloc>& x) { t.swap(x.t); }

  // insert/erase

  // 注意以下 insert ,返回的是动作的类型
  pair<iterator,bool> insert(const value_type& x) { return t.insert_unique(x); }
  iterator insert(iterator position, const value_type& x) {
    return t.insert_unique(position, x);
  }
#ifdef __STL_MEMBER_TEMPLATES
  template <class InputIterator>
  void insert(InputIterator first, InputIterator last) {
    t.insert_unique(first, last);
  }
#else
  void 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); }
  size_type erase(const key_type& x) { return t.erase(x); }
  void erase(iterator first, iterator last) { t.erase(first, last); }
  void clear() { t.clear(); }

  // map operations:

  iterator find(const key_type& x) { return t.find(x); }
  const_iterator find(const key_type& x) const { return t.find(x); }
  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&);
};

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;
}

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

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:
</span>


### 回答1: STL,全称为Standard Template Library,是C++标准库中的一部分,包含了许多常用的算法和数据结构实现。《STL源码剖析》是一本对STL源码进行深入解读和剖析的经典著作,其简体中文版对于广大C++程序员来说是非常实用和重要的参考资料。 本书分为六个部分。第一部分介绍了一些STL的基本概念和编程技巧,如迭代器、容器、算法等。第二部分讲解了STL的6个容器类型,包括vector、deque、list、set、map和hash_table。第三部分介绍了STL中的算法,其中涵盖了非常基础的排序、查找、拷贝、替换等操作,以及涉及到了迭代器和函数对象等高级内容。第四部分是有关迭代器的各种细节,包括iterator、reverse_iterator、const_iterator、const_reverse_iterator等。第五部分则主要讲解了仿函数的概念与应用,以及STL中提供的各种内置函数对象。最后一部分是STL的内部实现,作者为大家深入浅出地讲解了STL的各个组件如何实现、构造和优化。 本书的特点是结合许多例子和注释进行讲解,既有理论又有实践,让C++程序员能够深入了解和掌握STL的实现原理。此外,本书也对UNIX和Windows平台下的STL源码进行了比较和分析,对不同平台下的STL源码进行了对比,使读者能够很好地理解STL的跨平台兼容性。 总之,《STL源码剖析》简体中文版是一本非常精彩的书籍,对于学习和研究C++标准库的程序员来说是一本非常实用的参考资料。 ### 回答2: STL(标准模板库)是C++编程语言中的一个重要工具,它提供了一些基础的数据结构和算法的实现,为程序员提供了方便和便捷的工具,同时也促进了C++程序的效率和可读性。 STL源码剖析简体中文版是一本专门介绍STL源码的书籍。本书对STL的各个组成部分——如容器、迭代器、算法等——进行了详细的分析和解释,并用通俗易懂的语言和大量的实例帮助读者深入理解和掌握STL的内部实现。 在本书中,作者首先介绍了STL的概念和基本特性,然后逐步深入讲解各个模块的实现原理:容器的底层实现、迭代器的种类和应用、算法的实现方法等。特别值得一提的是,本书还包含了对常见算法的性能测试和比较,使读者可以更加清晰地了解每个算法的使用场景和优缺点。 本书的作者是STL的专家之一,他在对STL库进行研究和开发的过程中积累了丰富的经验和知识。他用简洁明了的语言和生动的实例解释了STL的各个模块的实现原理,使读者可以更加深入地理解和熟练掌握STL的使用方法。 总之,《STL源码剖析简体中文版》是一本非常优秀的STL教材,对于想要深入学习和掌握STL库的C++程序员来说,是一本不可多得的好书。 ### 回答3: STL(标准模板库)是C++中的一个重要部分,是由许多通用数据结构和算法组成的模板库。STL提供了一组模板类,包括向量、列表、哈希表、堆、映射等,以及与之相关的一系列算法,例如排序、查找和合并等。 《STL源码剖析》是一本介绍STL实现细节的书籍,它深入探讨了STL的实现原理、内部数据结构以及概述了STL库的各个组件。本书分为四个部分,分别是基础篇、容器篇、迭代器篇、算法篇,涵盖了STL库的方方面面。 基础篇介绍了STL的起源和设计思路,还介绍了STL各个组件之间的关系。容器篇主要介绍了STL提供的容器类,包括vector、list、set、map等常见的数据结构的内部实现和应用,以及容器的迭代器的使用方法。迭代器篇主要介绍了各种迭代器的使用方法,包括输入、输出、正向、双向和随机访问迭代器等。算法篇则介绍了STL提供的各种算法,包括排序、查找、复制、替换、合并等。 总体来说,《STL源码剖析》简体中文版是一本很好的探索STL内部实现的书籍。通过研究STL源码的实现原理,可以更好地理解C++编程语言的特性和其底层机制,从而更好地应用C++开发高质量的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值