Set:
是来做二叉搜索树(key)的事情。
包含头文件#include
具体的用法如下:
void test1()
{
set s1;//实现的是排序+去重
s1.insert(1);
s1.insert(3);
s1.insert(2);
s1.insert(11);
s1.insert(1);
set::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it;//结果是1,2,3,11
++it;
}
s1.erase(11);//删除
auto pos = s1.find(11);//查找
vector num;
num = { 1,2,3,4,5,6 };
s1.insert(num.begin(), num.end());//set也可以这样插入
}
map:
是来做二叉搜索树(key/value)的事情。
包含头文件#include
具体用法:
void test2()
{
map dict;
pair kv1("sort", "排序");//insert传的参数类型是value_type,value_type是模板 --pair. K就是key,T就是value
dict.insert(kv1);//所以insert这样传参。
dict.insert(pair("sort", "排序"));//这样也是可以的
dict.insert(make_pair("right", "右边"));//这样也是可以的
dict.insert({ "string","字符串" });这样也是可以的,隐式类型转换。
map ::iterator it = dict.begin();
//auto it = dict.begin();
while (it != dict.end())
{
cout << (*it).first<<":"<< (*it).second;//pair不支持流插入,
cout << it->first << ":" << it->second;
++it;
}
for (auto& e : dict)
{
cout << e.first << ";" << e.second;
}
}
map和set底层就是红黑树
红黑树的代码:
#pragma once
#include
enum Colour
{
RED,
BLACK
};
template
struct RBTreeNode
{
RBTreeNode* _left;
RBTreeNode* _right;
RBTreeNode* _parent;
T _data;
Colour _col;
RBTreeNode(const T& data)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _data(data)
, _col(RED)
{}
};
template
struct __RBTreeIterator
{
typedef RBTreeNode Node;
typedef __RBTreeIterator Self;
Node* _node;
__RBTreeIterator(Node* _node)//构造函数
:_node(node)
{
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
Self& operator++()//使用的是中序遍历(左 根 右),
{
//这里有两种情况:1.当前节点的右子树不为空,++就是右子树的最左节点。2.当前节点的右子树为空,++就是倒着在祖先里找,找孩子是父亲的左边
if (_node->_right)
{
Node* leftmin = _node->_right;
while (leftmin->_left)
{
leftmin = leftmin->_left;
}
_node = leftmin;
}
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = parent->_parent;
}
_node = parent;
}
return *this;
}
};
template//传KeyOfT是因为set的key就是k,但是map的key是pair的第一个。这是根据类型T判断的的,T是pair/K
class RBTree
{
typedef RBTreeNode Node;
public:
typedef __RBTreeIterator Iterator;
typedef __RBTreeIterator ConstIterator;//const迭代器
RBTree(const RBTree& t)//拷贝构造
{
_root = Copy(t._root);
}
// t2 = t1
RBTree& operator=(RBTree t)//赋值
{
swap(_root, t._root);
return *this;
}
~RBTree()
{
Destroy(_root);
_root = nullptr;
}
Iterator Begin()//左子树的最左边
{
Node* leftmin = _root;
while (leftmin && leftmin->_left)
{
leftmin = leftmin->_left;
}
return Iterator(leftmin);//相当于是调用了迭代器构造函数。返回这个节点。
}
Iterator End()
{
return Iterator(nullptr);
}
ConstIterator End() const
{
return ConstIterator(nullptr);
}
ConstIterator Begin() const
{
Node* leftMin = _root;
while (leftMin && leftMin->_left)
{
leftMin = leftMin->_left;
}
return ConstIterator(leftMin);
}
pair Insert(const T& data)//插入,当列表没有的时候,新增,已经存在的直接count++;
{
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)
{
// K
// pair
// kot对象,是用来取T类型的data对象中的key
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);
}
}
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;
// parent的颜色是黑色也结束
while (parent && parent->_col == RED)
{
// 关键看叔叔
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
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)
{
// g
// p u
// c
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g
// p u
// c
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
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 // 叔叔不存在,或者存在且为黑
{
// 情况二:叔叔不存在或者存在且为黑
// 旋转+变色
// g
// u p
// c
if (cur == parent->_right)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g
// u p
// c
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return make_pair(Iterator(newnode), true);
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* ppNode = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
Node* ppNode = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (ppNode->_right == parent)
{
ppNode->_right = subR;
}
else
{
ppNode->_left = subR;
}
subR->_parent = ppNode;
}
}
void InOrder()//遍历
{
_InOrder(_root);
cout << endl;
}
private:
Node* Copy(Node* root)//实现深拷贝,用前序遍历
{
if (root == nullptr)
{
return nullptr;
}
Node* newnode = new Node(root->_data);
newnode->_col = root->_col;
Node* left = Copy(root->_left)
if (left)
{
newnode->_left->_parent = newnode;
}
Node* right = Copy(root->_right)
if (left)
{
newnode->_right->_parent = newnode;
}
return newnode;
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_InOrder(root->_right);
}
private:
Node* _root = nullptr;
};
封装的set:
template
class Set
{
struct SetKeyOfT
{
const K& operator()(const K & key)
{
return key;
}
}
public:
typedef typename RBTree::Iterator iterator;
typedef typename RBTree::ConstIterator const_iterator;
const_iterator begin() const
{
return _t.Begin();
}
const_iterator end() const
{
return _t.End();
}
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
pair insert(const K& key)
{
return _t.Insert(key);
}
private:
RBTree _t;
};
封装的map:
template
class Map
{
struct MapKeyOfT(const pair& kv)
{
const K& operator()(const pair&kv)
{
return kv.first;
}
}
public:
typedef typename RBTree, MapKeyOfT>::Iterator iterator;
typedef typename RBTree::ConstIterator const_iterator;
const_iterator begin() const
{
return _t.Begin();
}
const_iterator end() const
{
return _t.End();
}
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
pair insert(const pair& kv)
{
return _t.Insert(key);
}
V& operator[](const K& key)//返回的是value的值。
{
pair ret = _t.Insert(make_pair(key, V()));
return ret.first->second;
}
private:
RBTree _t;
};
这里重点说一下运算符重载[ ]:
map运算符重载函数为
mapped_type& operator[] (const key_type& k) {
return (*((this->insert(make_pair(k, mapped_type()))).first)).second;
}
可以变为下面的代码,清晰易懂:
mapped_type& operator[] (const key_type& k) {
pair
// return (*(ret.first)).second;
return ret.first->second;
}
如果元素不存在,则插入一个具有默认值的新元素,并返回对这个元素的引用。首先 const key_type& k 是传入的键,表示要查找或插入的元素的键值。make_pair(k, mapped_type()) 创建了一个 pair 对象,该对象的第一个元素是传入的键 k,第二个元素是使用 mapped_type 的默认构造函数创建的默认值。insert调用容器的 insert 函数,该函数用于将一个键-值对插入容器中。返回值是一个 pair,pair的 first 成员指向插入的元素,second 成员指示是否插入成功。ret.first是pair中的第一个对象,也就是迭代器。ret.first->second是迭代器中的value的值。