【C++练级之路】【Lv.17】【STL】set类和map类的模拟实现

简介: 用改造后的红黑树,模拟实现set和map
远方有一堆篝火,在为久候之人燃烧!

@[TOC]

引言

STL库中的set类和map类,其底层原理都是==通过红黑树来实现==的。尽管set和map可以各自实现一棵红黑树,但是为了提高代码的复用率,STL库中将红黑树进行了一定的改造,实现==以相同的底层实现不同的容器==。

一、红黑树(改造版)

1.1 结点

enum Color
{
   
   
    RED,
    BLACK
};

template<class T>
struct RBTreeNode
{
   
   
    RBTreeNode<T>* _left;
    RBTreeNode<T>* _right;
    RBTreeNode<T>* _parent;
    T _data;
    Color _col;

    RBTreeNode(const T& data)
        : _left(nullptr)
        , _right(nullptr)
        , _parent(nullptr)
        , _data(data)
        , _col(RED)
    {
   
   }
};

细节:

  • 将==数据类型改为T==,因为要同时适用set(存储键值)和map(存储键值对)

1.2 迭代器

改造后的红黑树,最重要的功能之一就是支持双向迭代器,以最左结点为首,以最右结点为尾。

template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
   
   
    typedef RBTreeNode<T> Node;
    typedef RBTreeIterator<T, T&, T*> Iterator;
    typedef RBTreeIterator<T, Ref, Ptr> Self;
    Node* _node;

    RBTreeIterator(Node* node)
        : _node(node)
    {
   
   }

    RBTreeIterator(const Iterator& it)
        : _node(it._node)
    {
   
   }

    Ref operator*()
    {
   
   
        return _node->_data;
    }

    Ptr operator->()
    {
   
   
        return &(operator*());
    }

    bool operator!=(const Self& s)
    {
   
   
        return _node != s._node;
    }

    bool operator==(const Self& s)
    {
   
   
        return _node == s._node;
    }
};

细节:

  1. 一些基本的迭代器范式操作已经给出,重点的++与- -操作后面详细实现
  2. 迭代器的拷贝构造函数有两个用途:
    • 以普通迭代器拷贝出普通迭代器(普通迭代器调用时)
    • ==以普通迭代器拷贝出const迭代器==(const迭代器调用时)

1.2.1 operator++

Self& operator++()
{
   
   
    if (_node->_right)//右不为空,找右子树的最左结点
    {
   
   
        Node* subLeft = _node->_right;
        while (subLeft->_left)
        {
   
   
            subLeft = subLeft->_left;
        }
        _node = subLeft;
    }
    else//右为空,向上找孩子是父亲左的那个父亲
    {
   
   
        Node* parent = _node->_parent;
        Node* cur = _node;
        while (parent && parent->_right == cur)
        {
   
   
            cur = parent;
            parent = cur->_parent;
        }
        _node = parent;
    }
    return *this;
}

Self operator++(int)
{
   
   
    Self tmp = *this;
    ++*this;
    return tmp;
}

细节:

  1. 前置++的思路:
    • 右不为空,找右子树的最左结点
    • 右为空,向上找孩子是父亲左的那个父亲
  2. 后置++:复用前置++,返回临时对象

1.2.2 operator- -

Self& operator--()
{
   
   
    if (_node->_left)//左不为空,找左子树的最右结点
    {
   
   
        Node* subRight = _node->_left;
        while (subRight->_right)
        {
   
   
            subRight = subRight->_right;
        }
        _node = subRight;
    }
    else//左为空,向上找孩子是父亲右的那个父亲
    {
   
   
        Node* parent = _node->_parent;
        Node* cur = _node;
        while (parent && parent->_left == cur)
        {
   
   
            cur = parent;
            parent = cur->_parent;
        }
        _node = parent;
    }
    return *this;
}

Self operator--(int)
{
   
   
    Self tmp = *this;
    --*this;
    return tmp;
}

细节:

  1. 前置- -的思路:
    • 左不为空,找左子树的最右结点
    • 左为空,向上找孩子是父亲右的那个父亲
  2. 后置- -:复用前置- -,返回临时对象

1.3 本体

1.3.1 成员变量

template<class K, class T, class KeyOfT>
class RBTree
{
   
   
protected:
    typedef RBTreeNode<T> Node;
public:
protected:
    Node* _root = nullptr;
};

细节:

  1. 模板参数第一个为K,键值类型(比较时会用到)
  2. 模板参数第二个为T,同时适用set(存储键值)和map(存储键值对)
  3. 模板参数第三个为KeyOfT(仿函数类型),用于==获取不同数据T的键值key==来进行比较

1.3.2 begin和end

typedef RBTreeIterator<T, T&, T*> iterator;
typedef RBTreeIterator<T, const T&, const T*> const_iterator;

iterator begin()
{
   
   
    Node* cur = _root;
    while (cur->_left)
    {
   
   
        cur = cur->_left;
    }
    return iterator(cur);
}

const_iterator begin() const
{
   
   
    Node* cur = _root;
    while (cur->_left)
    {
   
   
        cur = cur->_left;
    }
    return const_iterator(cur);
}

iterator end()
{
   
   
    return iterator(nullptr);
}

const_iterator end() const
{
   
   
    return const_iterator(nullptr);
}

细节:begin返回最左节点的迭代器,end返回空迭代器

1.3.3 Find

iterator Find(const K& key)
{
   
   
    if (_root == nullptr)
    {
   
   
        return iterator(nullptr);
    }

    KeyOfT kot;
    Node* cur = _root;
    while (cur)
    {
   
   
        if (kot(cur->_data) < key)
        {
   
   
            cur = cur->_right;
        }
        else if (kot(cur->_data) > key)
        {
   
   
            cur = cur->_left;
        }
        else
        {
   
   
            return iterator(cur);
        }
    }

    return iterator(nullptr);
}

细节:

  1. 返回迭代器
  2. 运用仿函数进行键值比较

1.3.4 Insert

pair<iterator, bool> Insert(const T& data)
{
   
   
    if (_root == nullptr)
    {
   
   
        _root = new Node(data);
        _root->_col = BLACK;
        return make_pair(iterator(_root), true);
    }

    KeyOfT kot;
    Node* parent = nullptr;
    Node* cur = _root;
    while (cur)
    {
   
   
        if (kot(cur->_data) < kot(data))
        {
   
   
            parent = cur;
            cur = cur->_right;
        }
        else if (kot(cur->_data) > kot(data))
        {
   
   
            parent = cur;
            cur = cur->_left;
        }
        else
        {
   
   
            return make_pair(iterator(cur), false);
        }
    }

    Node* newnode = new Node(data);
    cur = newnode;
    if (kot(parent->_data) < kot(data))
    {
   
   
        parent->_right = cur;
    }
    else
    {
   
   
        parent->_left = cur;
    }
    cur->_parent = parent;

    while (parent && parent->_col == RED)
    {
   
   
        Node* grandparent = parent->_parent;
        if (grandparent->_right == parent)//uncle在左,parent在右
        {
   
   
            Node* uncle = grandparent->_left;
            if (uncle && uncle->_col == RED)//uncle为红,变色+向上调整
            {
   
   
                parent->_col = uncle->_col = BLACK;
                grandparent->_col = RED;

                cur = grandparent;
                parent = cur->_parent;
            }
            else//uncle为空或为黑,变色+旋转
            {
   
   
                if (parent->_right == cur)//左单旋
                {
   
   
                    RotateL(grandparent);
                    parent->_col = BLACK;
                    grandparent->_col = RED;
                }
                else//右左旋
                {
   
   
                    RotateR(parent);
                    RotateL(grandparent);
                    cur->_col = BLACK;
                    grandparent->_col = RED;
                }
            }
        }
        else//parent在左,uncle在右
        {
   
   
            Node* uncle = grandparent->_right;
            if (uncle && uncle->_col == RED)
            {
   
   
                parent->_col = uncle->_col = BLACK;
                grandparent->_col = RED;

                cur = grandparent;
                parent = cur->_parent;
            }
            else
            {
   
   
                if (parent->_left == cur)//右单旋
                {
   
   
                    RotateR(grandparent);
                    parent->_col = BLACK;
                    grandparent->_col = RED;
                }
                else//左右旋
                {
   
   
                    RotateL(parent);
                    RotateR(grandparent);
                    cur->_col = BLACK;
                    grandparent->_col = RED;
                }
            }
        }
    }
    _root->_col = BLACK;

    return make_pair(iterator(newnode), true);
}

细节:

  1. 返回pair,第一个参数为迭代器,第二个参数为布尔值(记录是否插入成功)
  2. 运用仿函数进行键值比较

二、set

2.1 成员变量与仿函数

template<class K>
class set
{
   
   
    struct SetKeyOfT
    {
   
   
        const K& operator()(const K& key)
        {
   
   
            return key;
        }
    };
public:
protected:
    RBTree<K, K, SetKeyOfT> _t;
};

细节:

  1. set类仿函数,直接返回参数key
  2. 成员变量的第二个模板参数为K,第三个模板参数为SetKeyOfT

    2.2 begin和end

typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator;
typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;

iterator begin()
{
   
   
    return _t.begin();
}

const_iterator begin() const
{
   
   
    return _t.begin();
}

iterator end()
{
   
   
    return _t.end();
}

const_iterator end() const
{
   
   
    return _t.end();
}

细节:

  1. 加上typename关键字,编译器才能识别类型
  2. set中存储的键值key均不允许修改,所以其普通迭代器和const迭代器均为红黑树的const迭代器
  3. 由于set的普通迭代器也是红黑树的const迭代器,调用普通begin()时,便有==从普通迭代器到const迭代器的转换==,此时之前写的拷贝构造(以普通迭代器拷贝构造const迭代器)便派上用场了。

2.3 find

iterator find(const K& key)
{
   
   
    return _t.Find(key);
}

2.4 insert

pair<iterator, bool> insert(const K& key)
{
   
   
    return _t.Insert(key);
}

细节:

  1. 插入参数类型为K(键值)
  2. 此时也有从普通迭代器到const迭代器的转换

三、map

3.1 成员变量与仿函数

template<class K, class V>
class map
{
   
   
    struct MapKeyOfT
    {
   
   
        const K& operator()(const pair<const K, V>& kv)
        {
   
   
            return kv.first;
        }
    };
public:
protected:
    RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};

细节:

  1. map类仿函数,返回参数pair的first
  2. 成员变量的第二个模板参数为pair,第三个模板参数为MapKeyOfT

3.2 begin和end

typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::iterator iterator;
typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::const_iterator const_iterator;

iterator begin()
{
   
   
    return _t.begin();
}

const_iterator begin() const
{
   
   
    return _t.begin();
}

iterator end()
{
   
   
    return _t.end();
}

const_iterator end() const
{
   
   
    return _t.end();
}

细节:

  1. 加上typename关键字,编译器才能识别类型
  2. map同样不允许修改key,故加上const修饰,但是允许修改存储的value,所以普通和const迭代器一一对应

此时,可能有人会问,那刚刚set不允许修改key,为什么不也直接用const修饰呢?请看以下这段代码:

typedef RBTreeIterator<T, const T&, const T*> const_iterator;

如果变成第二个模板参数T传入const K,那么就会形成两个连续的const,这是不被允许的。所以才想了其他方法来补救。

3.3 find

iterator find(const K& key)
{
   
   
    return _t.Find(key);
}

3.4 insert

pair<iterator, bool> insert(const pair<const K, V>& kv)
{
   
   
    return _t.Insert(kv);
}

细节:插入参数类型为pair(键值对)

3.5 operator[ ]

map最好用的重载运算符[ ],我们肯定也要实现,平常插入和修改使用[ ]更加方便。

V& operator[](const K& key)
{
   
   
    pair<iterator, bool> ret = _t.Insert(make_pair(key, V()));
    return ret.first->second;
}

细节:

  1. 插入成功便是插入,插入失败便是查找+修改
  2. 返回value的引用,可以直接插入或修改


真诚点赞,手有余香


相关文章
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
523 2
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
1022 73
|
存储 算法 C++
【c++丨STL】set/multiset的使用
本文深入解析了STL中的`set`和`multiset`容器,二者均为关联式容器,底层基于红黑树实现。`set`支持唯一性元素存储并自动排序,适用于高效查找场景;`multiset`允许重复元素。两者均具备O(logN)的插入、删除与查找复杂度。文章详细介绍了构造函数、迭代器、容量接口、增删操作(如`insert`、`erase`)、查找统计(如`find`、`count`)及`multiset`特有的区间操作(如`lower_bound`、`upper_bound`、`equal_range`)。最后预告了`map`容器的学习,其作为键值对存储的关联式容器,同样基于红黑树,具有高效操作特性。
902 3
|
存储 C++ 容器
【C++】map、set基本用法
本文介绍了C++ STL中的`map`和`set`两种关联容器。`map`用于存储键值对,每个键唯一;而`set`存储唯一元素,不包含值。两者均基于红黑树实现,支持高效的查找、插入和删除操作。文中详细列举了它们的构造方法、迭代器、容量检查、元素修改等常用接口,并简要对比了`map`与`set`的主要差异。此外,还介绍了允许重复元素的`multiset`和`multimap`。
596 3
【C++】map、set基本用法
|
存储 算法 C++
【C++】unordered_map(set)
C++中的`unordered`容器(如`std::unordered_set`、`std::unordered_map`)基于哈希表实现,提供高效的查找、插入和删除操作。哈希表通过哈希函数将元素映射到特定的“桶”中,每个桶可存储一个或多个元素,以处理哈希冲突。主要组成部分包括哈希表、哈希函数、冲突处理机制、负载因子和再散列,以及迭代器。哈希函数用于计算元素的哈希值,冲突通过开链法解决,负载因子控制哈希表的扩展。迭代器支持遍历容器中的元素。`unordered_map`和`unordered_set`的插入、查找和删除操作在理想情况下时间复杂度为O(1),但在冲突较多时可能退化为O(n)。
761 5
|
存储 C++ 容器
【C++】map的模拟实现
C++中的`map`是STL中的一种关联容器,存储键值对且键唯一。`map`基于红黑树实现,自动按键排序,支持动态调整、复杂数据类型、丰富的成员函数及双向迭代器。插入、查找等操作保证了对数时间复杂度,适用于需要快速查找和有序存储的场景。
362 3
|
存储 C++ 容器
【C++】set模拟实现
C++中的`set`是STL提供的一种关联容器,用于存储唯一元素并自动按特定顺序(默认升序)排序。其内部通过红黑树实现,保证了高效的插入、删除和查找操作,时间复杂度均为O(log n)。`set`支持迭代器遍历,提供了良好的数据访问接口。
393 3
|
存储 Java API
详细解析HashMap、TreeMap、LinkedHashMap等实现类,帮助您更好地理解和应用Java Map。
【10月更文挑战第19天】深入剖析Java Map:不仅是高效存储键值对的数据结构,更是展现设计艺术的典范。本文从基本概念、设计艺术和使用技巧三个方面,详细解析HashMap、TreeMap、LinkedHashMap等实现类,帮助您更好地理解和应用Java Map。
491 3
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。