序列式容器和关联式容器
string、vector、list、deque、array、forward_list,这些容器统称为序列式容器
这些容器存储之间的值一般没有紧密的关联关系,比如交换一下,它依然是序列式容器
map、set这些系列的就是关联式容器,两个位置有紧密的关联关系,交换一下它的结构就被破坏了
map和set的底层是红黑树
set是key搜索场景的结构
map是key/value搜索场景的结构
set类
template < class T, class Compare = less<T>, class Alloc = allocator<T>>
class set
{};
T是set底层关键字key的类型
set默认是小于进行比较,如果想按照自己的想法来比较可以给第二个参数传一个仿函数
set底层存储数据的内存是从空间配置器申请的,如果需要可以自己实现内存池传给第三个参数
一般情况下只需要传第一个参数即可
multiset和set的差异
multiset和set的用法基本完全类似,主要的区别就在于multiset支持值冗余,也就是multiset能有多个相同的key,但是set不能有相同的key
map类
template<class Key, class T, class Compare = less<Key>, class Alloc = allocator<pair<const Key, T> >map::allocator_type>
class map
{};
Key是map底层关键字的类型
T是map底层value的类型
map和set一样也是默认按key小于比较,如果想按照自己的想法来比较可以给第二个参数传一个仿函数
可以自己实现内存池传给第三个参数
multimap和map的差异
multimap和map的用法基本完全类似,主要的区别就在于multimap支持值冗余,也就是multimap能有多个相同的key,但是map不能有相同的key
map和set类的模拟实现
红黑树结构
template<class T>
struct RBTreeNode
{T _data;RBTreeNode<T>* _left;RBTreeNode<T>* _right;RBTreeNode<T>* _parent;Colour _col;RBTreeNode(const T& data):_data(data), _left(nullptr), _right(nullptr), _parent(nullptr){}
};
结构里和之前一样,这里的数据写的T是为了适配map和set两种容器
如果是map,则T应该是pair<key, value>,如果是set,则T应该是key
迭代器
template<class T, class Ref, class Ptr>
struct RBTreeIterator
{typedef RBTreeNode<T> Node;typedef RBTreeIterator<T, Ref, Ptr> Self;Node* _node;Node* _root; // 删除的--end需要根节点RBTreeIterator(Node* node, Node* root):_node(node),_root(root){}Self operator++(){if (_node->_right){Node* min = _node->_right;while (min->_left){min = min->_left;}_node = min;}else{Node* cur = _node;Node* parent = cur->_parent;while (parent && parent->_right == cur){cur = parent;parent = cur->_parent;}_node = parent;}return *this;}Self operator--(){if (_node == nullptr) // end(){// root的最右节点Node* cur = _root;while (cur && cur->_right){cur = cur->_right;}_node = cur;}else if (_node->_left){Node* min = _node->_left;while (min->_right){min = min->_right;}_node = min;}else{Node* cur = _node;Node* parent = cur->_parent;while (parent && parent->_left == cur){cur = parent;parent = cur->_parent;}_node = parent;}return *this;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}bool operator!=(const Self& s) const{return _node != s._node;}bool operator==(const Self& s) const{return _node == s._node;}
};
operator++
这里的++分为两种情况
若当前节点存在右子树,则应该找右子树的最左节点
若不存在右子树,则需要往上找parent,直到当前节点不再是parent的右节点为止
operator--
和operator++的情况相反
若当前节点存在左子树,则应该找左子树的最右节点
若不存在左子树,则需要往上找parent,直到当前节点不再是parent的左节点为止
map的operator[]
V& operator[](const K& key)
{pair<iterator, bool> ret = insert({ key, V() });return ret.first->second;
}
map的[]需要支持插入、修改两种功能
RBTree中insert的返回值为pair<iterator, bool>
返回插入节点迭代器里面的second即可
完整代码
RBTree.h
#pragma onceenum Colour
{RED,BLACK
};template<class T>
struct RBTreeNode
{T _data;RBTreeNode<T>* _left;RBTreeNode<T>* _right;RBTreeNode<T>* _parent;Colour _col;RBTreeNode(const T& data):_data(data), _left(nullptr), _right(nullptr), _parent(nullptr){}
};template<class T, class Ref, class Ptr>
struct RBTreeIterator
{typedef RBTreeNode<T> Node;typedef RBTreeIterator<T, Ref, Ptr> Self;Node* _node;Node* _root; // 删除的--end需要根节点RBTreeIterator(Node* node, Node* root):_node(node),_root(root){}Self operator++(){if (_node->_right){Node* min = _node->_right;while (min->_left){min = min->_left;}_node = min;}else{Node* cur = _node;Node* parent = cur->_parent;while (parent && parent->_right == cur){cur = parent;parent = cur->_parent;}_node = parent;}return *this;}Self operator--(){if (_node == nullptr) // end(){// root的最右节点Node* cur = _root;while (cur && cur->_right){cur = cur->_right;}_node = cur;}else if (_node->_left){Node* min = _node->_left;while (min->_right){min = min->_right;}_node = min;}else{Node* cur = _node;Node* parent = cur->_parent;while (parent && parent->_left == cur){cur = parent;parent = cur->_parent;}_node = parent;}return *this;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}bool operator!=(const Self& s) const{return _node != s._node;}bool operator==(const Self& s) const{return _node == s._node;}
};template<class K, class T, class KeyOfT>
class RBTree
{typedef RBTreeNode<T> Node;
public:typedef RBTreeIterator<T, T&, T*> Iterator;typedef RBTreeIterator<T, const T&, const T*> ConstIterator;
public:Iterator Begin(){Node* cur = _root;while (cur && cur->_left){cur = cur->_left;}return Iterator(cur, _root);}Iterator End(){return Iterator(nullptr, _root);}ConstIterator Begin() const{Node* cur = _root;while (cur && cur->_left){cur = cur->_left;}return ConstIterator(cur, _root);}ConstIterator End() const{return ConstIterator(nullptr, _root);}pair<Iterator, bool> Insert(const T& data){if (_root == nullptr){_root = new Node(data);_root->_col = BLACK;return { Iterator(_root, _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 { Iterator(cur, _root), false };}}cur = new Node(data);Node* newnode = cur;cur->_col = RED;if (kot(parent->_data) < kot(data)){parent->_right = cur;}else{parent->_left = cur;}cur->_parent = parent;while (parent && parent->_col == RED){Node* grandfather = parent->_parent;if (grandfather->_left == parent){Node* uncle = grandfather->_right;if (uncle && uncle->_col == RED){// 变色parent->_col = uncle->_col = BLACK;grandfather->_col = RED;// 继续向上处理cur = grandfather;parent = cur->_parent;}else{if (cur == parent->_left){RotateR(grandfather);parent->_col = BLACK;grandfather->_col = RED;}else{RotateL(parent);RotateR(grandfather);cur->_col = BLACK;parent->_col = grandfather->_col = RED; // 1}break;}}else{Node* uncle = grandfather->_left;if (uncle && uncle->_col == RED){// 变色parent->_col = uncle->_col = BLACK;grandfather->_col = RED;// 继续向上处理cur = grandfather;parent = cur->_parent;}else{if (cur == parent->_right){RotateL(grandfather);parent->_col = BLACK;grandfather->_col = RED;}else{RotateR(parent);RotateL(grandfather);cur->_col = BLACK;parent->_col = grandfather->_col = RED; // 1}break;}}}// 确保根节点颜色是黑色_root->_col = BLACK;return { Iterator(newnode, _root), true };}void RotateR(Node* parent){Node* subL = parent->_left;Node* subLR = subL->_right;parent->_left = subLR;if (subLR)subLR->_parent = parent;Node* parentParent = parent->_parent;subL->_right = parent;parent->_parent = subL;if (parent == _root){_root = subL;subL->_parent = nullptr;}else{if (parentParent->_left == parent){parentParent->_left = subL;}else{parentParent->_right = subL;}subL->_parent = parentParent;}}void RotateL(Node* parent){Node* subR = parent->_right;Node* subRL = subR->_left;parent->_right = subRL;if (subRL)subRL->_parent = parent;Node* parentParent = parent->_parent;subR->_left = parent;parent->_parent = subR;if (parentParent == nullptr){_root = subR;subR->_parent = nullptr;}else{if (parent == parentParent->_left){parentParent->_left = subR;}else{parentParent->_right = subR;}subR->_parent = parentParent;}}int Height(){return _Height(_root);}int Size(){return _Size(_root);}Node* Find(const K& key){Node* cur = _root;while (cur){if (cur->_kv.first < key){cur = cur->_right;}else if (cur->_kv.first > key){cur = cur->_left;}else{return cur;}}return nullptr;}private:int _Height(Node* root){if (root == nullptr)return 0;int leftHeight = _Height(root->_left);int rightHeight = _Height(root->_right);return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;}int _Size(Node* root){if (root == nullptr)return 0;return _Size(root->_left) + _Size(root->_right) + 1;}private:Node* _root = nullptr;
};
Myset.h
#pragma once#include "RBTree.h"namespace lyw
{template<class K>class set{struct SetOfT{K operator()(const K& key){return key;}};public:typedef typename RBTree<K, const K, SetOfT>::Iterator iterator;typedef typename RBTree<K, const K, SetOfT>::ConstIterator const_iterator;iterator begin(){return _t.Begin();}iterator end(){return _t.End();}const_iterator begin() const{return _t.Begin();}const_iterator end() const{return _t.End();}pair<iterator, bool> insert(const K& key){return _t.Insert(key);}private:RBTree<K, const K, SetOfT> _t;};
}
Mymap.h
#pragma once#include "RBTree.h"namespace lyw
{template<class K, class V>class map{struct MapOfT{const K& operator()(const pair<K, V>& kv){return kv.first;}};public:typedef typename RBTree<K, pair<const K, V>, MapOfT>::Iterator iterator;typedef typename RBTree<K, pair<const K, V>, MapOfT>::ConstIterator const_iterator;iterator begin(){return _t.Begin();}iterator end(){return _t.End();}const_iterator begin() const{return _t.Begin();}const_iterator end() const{return _t.End();}pair<iterator, bool> insert(const pair<K, V>& kv){return _t.Insert(kv);}V& operator[](const K& key){pair<iterator, bool> ret = insert({ key, V() });return ret.first->second;}private:RBTree<K, pair<const K, V>, MapOfT> _t;};
}