万门cpp课程作业:std的map实现原理

先上一张侯捷老师的图
在这里插入图片描述
map基本就是对红黑树的重新包装
我们看代码:
map 的结构体定义:

template <class _Key, class _Tp, class _Compare, class _Alloc>
class map {
public:
    typedef _Key                  key_type; // 键值类型
    typedef _Tp                   data_type; // 实值类型
    typedef _Tp                   mapped_type;
    typedef pair<const _Key, _Tp> value_type;  // 元素类型(键值/实值)
    typedef _Compare              key_compare; // 键值比较函数

private:
    typedef _Rb_tree<key_type, value_type, 
                   _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
                    //重点: map 的底层机制 RB-tree 
    _Rep_type _M_t;  // red-black tree representing map 以红黑树(RB-tree) 表现 map
    
public:
    typedef typename _Rep_type::iterator iterator;  // map 迭代器
};

定义map 的比较操作

// map 比较操作
template <class _Key, class _Tp, class _Compare, class _Alloc>
inline bool operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  return __x._M_t == __y._M_t;
}

template <class _Key, class _Tp, class _Compare, class _Alloc>
inline bool operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
                      const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  return __x._M_t < __y._M_t;
}

#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER

template <class _Key, class _Tp, class _Compare, class _Alloc>
inline bool operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  return !(__x == __y);
}

template <class _Key, class _Tp, class _Compare, class _Alloc>
inline bool operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
                      const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  return __y < __x;
}

template <class _Key, class _Tp, class _Compare, class _Alloc>
inline bool operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  return !(__y < __x);
}

template <class _Key, class _Tp, class _Compare, class _Alloc>
inline bool operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  return !(__x < __y);
}

template <class _Key, class _Tp, class _Compare, class _Alloc>
inline void swap(map<_Key,_Tp,_Compare,_Alloc>& __x, 
                 map<_Key,_Tp,_Compare,_Alloc>& __y) {
  __x.swap(__y);
}

一些操作,交换元素,清除元素,lower_bound,upper_bound。

  void swap(map<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }

  // insert/erase
  
  // insert 插入键值对
  pair<iterator,bool> insert(const value_type& __x) 
    { return _M_t.insert_unique(__x); }
    
  iterator insert(iterator position, const value_type& __x)
    { return _M_t.insert_unique(position, __x); }
    
#ifdef __STL_MEMBER_TEMPLATES
  template <class _InputIterator>
  void insert(_InputIterator __first, _InputIterator __last) {
    _M_t.insert_unique(__first, __last);
  }
  
#else
  void insert(const value_type* __first, const value_type* __last) {
    _M_t.insert_unique(__first, __last);
  }
  
  void insert(const_iterator __first, const_iterator __last) {
    _M_t.insert_unique(__first, __last);
  }
  
#endif /* __STL_MEMBER_TEMPLATES */
  // 擦除元素 
  void erase(iterator __position) { _M_t.erase(__position); }
  
  size_type erase(const key_type& __x) { return _M_t.erase(__x); }
  
  void erase(iterator __first, iterator __last)
    { _M_t.erase(__first, __last); }
    
  void clear() { _M_t.clear(); }

  // map operations:
  // 寻找带有特定键的元素 
  iterator find(const key_type& __x) { return _M_t.find(__x); }
  
  const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
  
  size_type count(const key_type& __x) const {
    return _M_t.find(__x) == _M_t.end() ? 0 : 1; 
  }
  // 返回指向首个不小于给定关键的元素的迭代器
  iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
  
  const_iterator lower_bound(const key_type& __x) const {
    return _M_t.lower_bound(__x); 
  }
  // 返回指向首个大于给定关键的元素的迭代器 
  iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
  
  const_iterator upper_bound(const key_type& __x) const {
    return _M_t.upper_bound(__x); 
  }
  // 返回匹配特定键的元素范围 
  //这些函数大量使用红黑树的函数,要想看懂这些函数的底层实现,需要看红黑树的实现。
  
  pair<iterator,iterator> equal_range(const key_type& __x) {
    return _M_t.equal_range(__x);
  }
  
  pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
    return _M_t.equal_range(__x);
  }

很多实现都是直接用红黑树的函数,不需要自己再实现,增加数据类型判断。

发布了46 篇原创文章 · 获赞 0 · 访问量 828

猜你喜欢

转载自blog.csdn.net/github_38148039/article/details/103927652