C++_用红黑树实现关联容器map和set

将之前实现过的红黑树作为头文件,实现关联容器map和set。

myMap

template <class K,class V>
class Map
{
private:
 struct MapKeyOfValue
 {
  const  K& operator()(const pair<K, V>& data)
  {
   return data.first;
  }
 };
public:
 typedef typename RBTree<K, pair<K, V>, MapKeyOfValue>::iterator iterator;
 
 pair<iterator, bool> Insert(const pair<K, V>& data)
 {
  return _rbt.Insert(data);
 }
 iterator begin()
 {
  return _rbt.begin();
 }
 iterator end()
 {
  return _rbt.end();
 }
 V& operator[](const K& key)
 {
  pair<iterator, bool>ret = _rbt.Insert(make_pair(key, V()));
  return ret.first->second;
 }
private:
 RBTree<K, pair<K, V>,MapKeyOfValue> _rbt;
};

mySet

template <class K>
class Set
{
 struct SetKeyOfValue
 {
  const K& operator()(const K& data)
  {
   return data;
  }
 };
public:
 typedef typename RBTree<K, K, SetKeyOfValue>::iterator iterator;
 pair<iterator, bool>Insert(const K& data)
 {
  return _rbt.Insert(data);
 }
 iterator begin()
 {
  return _rbt.begin();
 }
 iterator end()
 {
  return _rbt.end();
 }
private:
 RBTree<K, K, SetKeyOfValue> _rbt;
};
发布了71 篇原创文章 · 获赞 131 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43239560/article/details/99866299