• 【C++】map,set简单操作的封装实现(利用红黑树)


    🌏博客主页: 主页
    🔖系列专栏: C++
    ❤️感谢大家点赞👍收藏⭐评论✍️
    😍期待与大家一起进步!



    一、STL中set与map的源码

    在这里插入图片描述

    在这里插入图片描述

    因为关联式容器中存储的是键值对,因此k为key的类型,
    ValueType: 如果是map,则为pair; 如果是set,则为k
    KeyOfValue: 通过value来获取key的一个仿函数

    二、 红黑树结点的意义

    我们知道map,和set需要用红黑树来实现,但我们map的数据类型是键值对pair类型,key的数据类型是单纯的K类型,那如何写出一个通用的红黑树模板呢?

    template<class T>//关键之处
    struct RBTreeNode {
    	RBTreeNode<T>* _left;
    	RBTreeNode<T>* _right;
    	RBTreeNode<T>* _parent;
    	Color _col;//结点颜色
    	T _data;
    	RBTreeNode(const T&data)
    		:_left(nullptr),
    		_right(nullptr),
    		_parent(nullptr),
    		_col(RED),
    		_data(data)
    	{}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    我们这里把pair看成一个整体,我们设计模板的时候就不需要考虑是不是键值对类型,需不需要多传一个模板参数的问题,达到了普适性。

    在map中,T传pair类型
    在set中,T传K类型

    三、仿函数的妙用

    我们value_type类型用模板参数T代替之后,这个时候就会衍生一个问题,我T可能为键值对类型,我键值对之间怎么比较呢?
    例如:T t1与T t2两个变量,我们肯定不能直接比较,肯定要依据他们的键值大小进行比较,所以我们需要自己写一个用于比较的函数,这个时候仿函数刚好能发挥这个用处,可以作为模板参数传入自己写的比较函数

    取出他们的键,让他们进行比较,这里set也这样写是为了配合map,因为两者都用的一个红黑树模板

    struct SetKeyOfT {
    			const K& operator()(const K&key) {
    				return key;
    			}
    		};
    
    
    struct MapKeyOfT
    		{
    			const K& operator()(const pair<K, V>& kv)
    			{
    				return kv.first;
    			}
    		};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    示例:红黑树中Find函数的实现:

    Node* Find(const K& key)
    	{
    		Node* cur = _root;
    		KeyOfT kot;//KeyOfT为仿函数的类型
    		//写好仿函数后先实例化出来
    		while (cur)
    		{
    			if (kot(cur->_data) < key)
    			{
    				cur = cur->_right;
    			}
    			else if (kot(cur->_data) > key)
    			{
    				cur = cur->_left;
    			}
    			else
    			{
    				return cur;
    			}
    		}
    		return nullptr;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    四、set,map定义迭代器的区别

    因为set插入进去后,set的值不可以被修改,为了实现这一操作我们可以在迭代器上下手

    //typename是告诉编译器这里后面跟的是类型不是对象,以免编译器报错
    typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator;
    typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;
    //既然不可修改,那我就都用const类型的迭代器
    
    • 1
    • 2
    • 3
    • 4

    在map中,我们是键不可修改,而其所对应的值可被修改,所以不能用set的那种操作,可以在传模板参数的时候动手脚,传pair的时候直接把K改为const类型

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

    五、map,set迭代器的基本操作:

    1.begin() end()

    iterator begin()
    	{
    		Node* leftMin = _root;
    		while (leftMin && leftMin->_left)
    		{
    			leftMin = leftMin->_left;
    		}
    
    		return iterator(leftMin);
    	}
    
    	iterator end()
    	{
    		return iterator(nullptr);
    	}
    
    	const_iterator begin() const
    	{
    		Node* leftMin = _root;
    		while (leftMin && leftMin->_left)
    		{
    			leftMin = leftMin->_left;
    		}
    
    		return const_iterator(leftMin);
    	}
    
    	const_iterator end() const
    	{
    		return const_iterator(nullptr);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    2.operator++

    在这里插入图片描述

    1.cur的右不为空访问右树的最左结点
    2.cur的右为空,找到cur是parent左子树的位置,此时parent的位置就是++后的位置

    Self& operator++()
    	{
    		if (_node->_right)
    		{
    			// 右树的最左节点(最小节点)
    			Node* subLeft = _node->_right;
    			while (subLeft->_left)
    			{
    				subLeft = subLeft->_left;
    			}
    
    			_node = subLeft;
    		}
    		else
    		{
    			Node* cur = _node;
    			Node* parent = cur->_parent;
    			// 找孩子是父亲左的那个祖先节点,就是下一个要访问的节点
    			while (parent && cur == parent->_right)
    			{
    				cur = cur->_parent;
    				parent = parent->_parent;
    			}
    
    			_node = parent;
    		}
    
    		return *this;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    3.operator–

    –就与++反着来
    1.左不为空,找到左树的最右结点
    2.左为空,找到cur是parent右的那个结点,此时parent的位置就是–之后的位置

    Self& operator--()
    	{
    		if (_node->_left)
    		{
    			Node* subRight = _node->_left;
    			while (subRight->_right)
    			{
    				subRight = subRight->_right;
    			}
    
    			_node = subRight;
    		}
    		else
    		{
    			// 孩子是父亲的右的那个节点
    			Node* cur = _node;
    			Node* parent = cur->_parent;
    			while (parent && cur == parent->_left)
    			{
    				cur = cur->_parent;
    				parent = parent->_parent;
    			}
    
    			_node = parent;
    		}
    
    		return *this;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    六、迭代器拷贝构造特殊处理

    template<class T, class Ptr, class Ref>
    struct __TreeIterator
    {
    	typedef RBTreeNode<T> Node;
    	typedef __TreeIterator<T, Ptr, Ref> Self;
    	
    	typedef __TreeIterator<T, T*, T&> Iterator;
    
    	__TreeIterator(const Iterator& it)
    		:_node(it._node)
    	{}
    
    	Node* _node;
    
    	__TreeIterator(Node* node)
    		:_node(node)
    	{}
    
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    1.当我们Ptr与Ref分别实例化为T与T&的时候,__TreeIterator(const Iterator& it)就是一个拷贝构造函数,因为Iterator与Self类型相同
    2.当我们Ptr与Ref分别实例化为const T
    与const T&的时候,__TreeIterator(const Iterator& it)是一个构造,支持普通迭代器构造const类型的迭代器因为Self为const类型,Iterator为普通类型
    这里支持用普通迭代器去构造const类型的迭代器,就可以满足我们set的插入功能的实现

    typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator;
    typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;
    pair<iterator,bool>insert(const K&key){
    pair<typename RBTree<K, K, SetKeyOfT>::iterator, bool> ret = _t.Insert(key);
    //这里RBTree里面的iterator类型为普通迭代器类型,而我们返回值里面的pair中的iterator为const类型,
    //所以要想返回必须先把RBTree中的iterator变为const类型,这个时候可以拷贝构造
    //让普通迭代器变为const类型的迭代器
    		return pair<iterator, bool>(ret.first, ret.second);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    7.源码

    这里会涉及到红黑树的一些变色问题,之前的博客有提到过【C++】红黑树插入操作实现以及验证红黑树是否正确
    需要的小伙伴可以去看一下

    RBTree.h

    #pragma once
    #include
    using namespace std;
    
    enum Color {
    	RED,
    	BLACK
    };
    
    
    template<class T>//关键之处
    struct RBTreeNode {
    	RBTreeNode<T>* _left;
    	RBTreeNode<T>* _right;
    	RBTreeNode<T>* _parent;
    	Color _col;//结点颜色
    	T _data;
    	RBTreeNode(const T&data)
    		:_left(nullptr),
    		_right(nullptr),
    		_parent(nullptr),
    		_col(RED),
    		_data(data)
    	{}
    };
    
    
    template<class T, class Ptr, class Ref>
    struct __TreeIterator
    {
    	typedef RBTreeNode<T> Node;
    	typedef __TreeIterator<T, Ptr, Ref> Self;
    	
    	typedef __TreeIterator<T, T*, T&> Iterator;
    
    	__TreeIterator(const Iterator& it)
    		:_node(it._node)
    	{}
    
    	Node* _node;
    
    	__TreeIterator(Node* node)
    		:_node(node)
    	{}
    
    	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;
    	}
    
    	Self& operator--()
    	{
    		if (_node->_left)
    		{
    			Node* subRight = _node->_left;
    			while (subRight->_right)
    			{
    				subRight = subRight->_right;
    			}
    
    			_node = subRight;
    		}
    		else
    		{
    			// 孩子是父亲的右的那个节点
    			Node* cur = _node;
    			Node* parent = cur->_parent;
    			while (parent && cur == parent->_left)
    			{
    				cur = cur->_parent;
    				parent = parent->_parent;
    			}
    
    			_node = parent;
    		}
    
    		return *this;
    	}
    
    	Self& operator++()
    	{
    		if (_node->_right)
    		{
    			// 右树的最左节点(最小节点)
    			Node* subLeft = _node->_right;
    			while (subLeft->_left)
    			{
    				subLeft = subLeft->_left;
    			}
    
    			_node = subLeft;
    		}
    		else
    		{
    			Node* cur = _node;
    			Node* parent = cur->_parent;
    			// 找孩子是父亲左的那个祖先节点,就是下一个要访问的节点
    			while (parent && cur == parent->_right)
    			{
    				cur = cur->_parent;
    				parent = parent->_parent;
    			}
    
    			_node = parent;
    		}
    
    		return *this;
    	}
    };
    
    
    template<class K,class T,class KeyOfT>
    class RBTree {
    	typedef RBTreeNode<T> Node;
    public:
    	// 同一个类模板,传的不同的参数实例化出的不同类型
    	typedef __TreeIterator<T, T*, T&> iterator;
    	typedef __TreeIterator<T, const T*, const T&> const_iterator;
    
    	iterator begin()
    	{
    		Node* leftMin = _root;
    		while (leftMin && leftMin->_left)
    		{
    			leftMin = leftMin->_left;
    		}
    
    		return iterator(leftMin);
    	}
    
    	iterator end()
    	{
    		return iterator(nullptr);
    	}
    
    	const_iterator begin() const
    	{
    		Node* leftMin = _root;
    		while (leftMin && leftMin->_left)
    		{
    			leftMin = leftMin->_left;
    		}
    
    		return const_iterator(leftMin);
    	}
    
    	const_iterator end() const
    	{
    		return const_iterator(nullptr);
    	}
    
    	Node* Find(const K& key)
    	{
    		Node* cur = _root;
    		KeyOfT kot;//KeyOfT为仿函数的类型
    		//写好仿函数后先实例化出来
    		while (cur)
    		{
    			if (kot(cur->_data) < key)
    			{
    				cur = cur->_right;
    			}
    			else if (kot(cur->_data) > key)
    			{
    				cur = cur->_left;
    			}
    			else
    			{
    				return cur;
    			}
    		}
    		return nullptr;
    	}
    
    
    	pair<iterator, bool> Insert(const T& data)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(data);
    			_root->_col = BLACK;
    			return make_pair(iterator(_root), true);
    		}
    
    		Node* parent = nullptr;
    		Node* cur = _root;
    
    		KeyOfT kot;
    		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);
    			}
    		}
    
    		cur = new Node(data);
    		cur->_col = RED;
    
    		Node* newnode = cur;
    
    		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 (parent == grandfather->_left)
    			{
    				Node* uncle = grandfather->_right;
    				// u存在且为红
    				if (uncle && uncle->_col == RED)
    				{
    					// 变色
    					parent->_col = uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					// 继续向上处理
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				else // u不存在 或 存在且为黑
    				{
    					if (cur == parent->_left)
    					{
    						//     g
    						//   p
    						// c
    						RotateR(grandfather);
    						parent->_col = BLACK;
    						grandfather->_col = RED;
    					}
    					else
    					{
    						//     g
    						//   p
    						//		c
    						RotateL(parent);
    						RotateR(grandfather);
    
    						cur->_col = BLACK;
    						grandfather->_col = RED;
    					}
    
    					break;
    				}
    			}
    			else // parent == grandfather->_right
    			{
    				Node* uncle = grandfather->_left;
    				// u存在且为红
    				if (uncle && uncle->_col == RED)
    				{
    					// 变色
    					parent->_col = uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					// 继续向上处理
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				else
    				{
    					if (cur == parent->_right)
    					{
    						// g
    						//	  p
    						//       c
    						RotateL(grandfather);
    						grandfather->_col = RED;
    						parent->_col = BLACK;
    					}
    					else
    					{
    						// g
    						//	  p
    						// c
    						RotateR(parent);
    						RotateL(grandfather);
    						cur->_col = BLACK;
    						grandfather->_col = RED;
    					}
    
    					break;
    				}
    			}
    		}
    
    		_root->_col = BLACK;
    
    		return make_pair(iterator(newnode), true);
    	}
    
    
    	void RotateL(Node* parent)
    	{
    		 
    
    		Node* cur = parent->_right;
    		Node* curleft = cur->_left;
    
    		parent->_right = curleft;
    		if (curleft)
    		{
    			curleft->_parent = parent;
    		}
    
    		cur->_left = parent;
    
    		Node* ppnode = parent->_parent;
    
    		parent->_parent = cur;
    
    
    		if (parent == _root)
    		{
    			_root = cur;
    			cur->_parent = nullptr;
    		}
    		else
    		{
    			if (ppnode->_left == parent)
    			{
    				ppnode->_left = cur;
    			}
    			else
    			{
    				ppnode->_right = cur;
    
    			}
    
    			cur->_parent = ppnode;
    		}
    	}
    
    
    	void RotateR(Node* parent)
    	{
    		 
    
    		Node* cur = parent->_left;
    		Node* curright = cur->_right;
    
    		parent->_left = curright;
    		if (curright)
    			curright->_parent = parent;
    
    		Node* ppnode = parent->_parent;
    		cur->_right = parent;
    		parent->_parent = cur;
    
    		if (ppnode == nullptr)
    		{
    			_root = cur;
    			cur->_parent = nullptr;
    		}
    		else
    		{
    			if (ppnode->_left == parent)
    			{
    				ppnode->_left = cur;
    			}
    			else
    			{
    				ppnode->_right = cur;
    			}
    
    			cur->_parent = ppnode;
    		}
    	}
    private:
    	Node* _root = nullptr;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406

    2.MyMap.h

    #pragma once
    #include"RBTree.h"
    namespace bit {
    	template<class K, class V>
    	class map
    	{
    		struct MapKeyOfT
    		{
    			const K& operator()(const pair<K, V>& kv)
    			{
    				return kv.first;
    			}
    		};
    
    
    	public:
    		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();
    		}
    
    		iterator end()
    		{
    			return _t.end();
    		}
    
    		const_iterator begin() const
    		{
    			return _t.begin();
    		}
    
    		const_iterator end() const
    		{
    			return _t.end();
    		}
    
    		V& operator[](const K& key)
    		{
    			pair<iterator, bool> ret = insert(make_pair(key, V()));
    			return ret.first->second;
    		}
    
    		pair<iterator, bool> insert(const pair<K, V>& kv)
    		{
    			return _t.Insert(kv);
    		}
    
    
    	private:
    		RBTree<K, pair<const K, V>, MapKeyOfT> _t;
    
    	};
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    3.MySet.h

    #pragma once
    #include"RBTree.h"
    
    
    namespace bit {
    	template<class K>
    	class set {
    		struct SetKeyOfT {
    			const K& operator()(const K&key) {
    				return key;
    			}
    		};
    
    
    	public:
    		typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator;
    		typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;
    
    		const_iterator begin() const
    		{
    			return _t.begin();
    		}
    
    		const_iterator end() const
    		{
    			return _t.end();
    		}
    
    		pair<iterator,bool>insert(const K&key){
    			pair<typename RBTree<K, K, SetKeyOfT>::iterator, bool> ret = _t.Insert(key);
    			return pair<iterator, bool>(ret.first, ret.second);
    	}
    
    
    	private:
    		RBTree<K, K, SetKeyOfT> _t;
    
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    Android学习-组件自动绑定
    深度学习——day19 读论文:基于改进 Sigmoid 卷积神经网络的手写体数字识别(2021 信息科技)
    LeetCode20.有效的括号
    我们真的需要链式查询吗?
    【Pytorch】torch.nn.Dropout()
    Java编程技巧:跨域
    2024年Q1季度平板电视行业线上市场销售数据分析
    行情订阅&分钟合成
    个人电脑可以当服务器用吗?
    FTP、FTPS与SFTP定义与联系
  • 原文地址:https://blog.csdn.net/m0_74774759/article/details/132923738