STL中map,multimap,set,multiset,unordered_map,unordered_multimap,unordered_set,unordered_multiset的实现方法

16 篇文章 0 订阅
10 篇文章 0 订阅

1. map, multimap, set, multiset

g++ 中 map, multimap, set, multiset 由红黑树实现

map: bits/stl_map.h

multimap: bits/stl_multimap.h

set: bits/stl_set.h

multiset: bits/stl_multiset.h

红黑树类——_Rb_tree: bits/stl_tree.h

若要分析红黑树的实现阅读 bits/stl_tree.h 即可


2. std::pair

位于 <utility>, 本质非常简单,就是一个含有 2 个成员的类模板:

// bits/stl_pair.h
template<class _T1, class _T2>
struct pair
{
    typedef _T1 first_type;    /// @c first_type is the first bound type
    typedef _T2 second_type;   /// @c second_type is the second bound type

    _T1 first;                 /// @c first is a copy of the first object
    _T2 second;                /// @c second is a copy of the second object
......
};

与之相关的比较重要的函数:

2.1 make_pair()

见例子

2.2 重载关系操作符

比较规则就是前面博文介绍的字典序比较(http://blog.csdn.net/duyiwuer2009/article/details/23277803),和 deque, list 等也类似,map 的比较是基于它的(因为 map 是存储着元素为 pair<key, value> 的容器,下文详细说明)。


3. map, set 的相似和本质

先看 gcc libstdc++ 实现代码:

// bits/stl_tree.h
template<typename _Key, typename _Val, typename _KeyOfValue,
        typename _Compare, typename _Alloc = allocator<_Val> >
class _Rb_tree
{
......
public:
    typedef _Key key_type;
    typedef _Val value_type;
......
};


// bits/stl_map.h
template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
        typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
class map
{
public:
    typedef _Key                         key_type;
    typedef _Tp                          mapped_type;
    typedef std::pair<const _Key, _Tp>   value_type;
    typedef _Compare                     key_compare;
......

public:
    class value_compare
    : public std::binary_function<value_type, value_type, bool>
    {
        friend class map<_Key, _Tp, _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); }
    };
......
    typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
                key_compare, _Pair_alloc_type> _Rep_type;

    /// The actual tree structure.
    _Rep_type _M_t;
......
};


// bits/stl_set.h
template<typename _Key, typename _Compare = std::less<_Key>,
        typename _Alloc = std::allocator<_Key> >
class set
{
......
    typedef _Key     key_type;
    typedef _Key     value_type;
    typedef _Compare key_compare;
    typedef _Compare value_compare;
......
    typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
                    key_compare, _Key_alloc_type> _Rep_type;
    _Rep_type _M_t;  // Red-black tree representing set.
......
};

可以看出:

1. map 的 value_type 为 std::pair<const _Key, _Tp>, 而 set 的 value_type 为 _Key, 都是通过 _Key 进行定位。

2. 从容器中存储元素的角度而言,关联容器与序列容器是一样的,只不过 map 存储的是 std::pair<const _Key, _Tp> (从 begin(), end() 返回的迭代器类型上也可以看出),区别是有个 key 与之关联。

3. 从用途上来说,set 相当于集合,方便剔除和增加新元素,map 自不用多说。


附:常见容器分类

序列容器(sequence container):

vector

list

deque

容器适配器(container adapter):

stack(last in, first out)

queue(first in, first out)

priority_queue

关联容器(associative container):

set

multiset

map

multimap


4. 红黑树

红黑树(red–black tree)是一种自平衡二叉查找树(self-balancing binary search tree),先看二叉查找树(BST)的定义和性质:

(1) 若它的左子树非空,则左子树上所有节点的值均小于它的根节点的值;

(2) 若它的右子树非空,则右子树上所有节点的值均大于(或大于等于)它的根节点的值;

(3) 它的左、右子树也分别为二叉查找树。(显然这是一个递归定义)


(4) 中序遍历(LDR: left subtree, current node(data), right subtree)将得到递增序列,所以又叫二叉排序树


5. unordered_map, unordered_multimap, unordered_set, unordered_multiset(C++ 11)

g++ 中 unordered_map, unordered_multimap, unordered_set, unordered_multiset 由哈希表实现

unordered_map       <-- __unordered_map      \
unordered_multimap  <-- __unordered_multimap -\
                                               | <-- std::_Hashtable(hashtable.h)
unordered_set       <-- __unordered_set      -/
unordered_multiset  <-- __unordered_multiset /

std::_Hashtable(hashtable.h) <-- std::__detail::_Rehash_base(hashtable_policy.h)







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值