博客 https://blog.lenyiin.com/set-map/ 的代码仓库
This commit is contained in:
parent
7abc18bfce
commit
73408f4b11
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -3,20 +3,20 @@
|
||||
"WorkspaceRootPath": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\",
|
||||
"Documents": [
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Set.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Set.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
}
|
||||
],
|
||||
"DocumentGroupContainers": [
|
||||
@ -26,7 +26,7 @@
|
||||
"DocumentGroups": [
|
||||
{
|
||||
"DockedWidth": 267,
|
||||
"SelectedChildIndex": 3,
|
||||
"SelectedChildIndex": 6,
|
||||
"Children": [
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
@ -42,20 +42,20 @@
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 0,
|
||||
"DocumentIndex": 3,
|
||||
"Title": "Main.cpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp",
|
||||
"RelativeDocumentMoniker": "Main.cpp",
|
||||
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp*",
|
||||
"RelativeToolTip": "Main.cpp*",
|
||||
"ViewState": "AgIAAF4BAAAAAAAAAAAswHEBAAABAAAAAAAAAA==",
|
||||
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp",
|
||||
"RelativeToolTip": "Main.cpp",
|
||||
"ViewState": "AgIAAHoBAAAAAAAAAAAswIUBAAAUAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
|
||||
"WhenOpened": "2024-09-23T15:54:15.721Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 1,
|
||||
"DocumentIndex": 2,
|
||||
"Title": "RBTree.hpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\RBTree.hpp",
|
||||
"RelativeDocumentMoniker": "RBTree.hpp",
|
||||
@ -68,26 +68,26 @@
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 2,
|
||||
"DocumentIndex": 1,
|
||||
"Title": "Set.hpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Set.hpp",
|
||||
"RelativeDocumentMoniker": "Set.hpp",
|
||||
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Set.hpp",
|
||||
"RelativeToolTip": "Set.hpp",
|
||||
"ViewState": "AgIAAC4AAAAAAAAAAAAswDYAAAAjAAAAAAAAAA==",
|
||||
"ViewState": "AgIAADkAAAAAAAAAAAAswEQAAAAJAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
|
||||
"WhenOpened": "2024-09-23T15:53:53.08Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 3,
|
||||
"DocumentIndex": 0,
|
||||
"Title": "Map.hpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp",
|
||||
"RelativeDocumentMoniker": "Map.hpp",
|
||||
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp",
|
||||
"RelativeToolTip": "Map.hpp",
|
||||
"ViewState": "AgIAAC4AAAAAAAAAAAAswEEAAAABAAAAAAAAAA==",
|
||||
"ViewState": "AgIAAD4AAAAAAAAAAAAswEgAAAAUAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
|
||||
"WhenOpened": "2024-09-23T15:53:43.888Z",
|
||||
"EditorCaption": ""
|
||||
|
@ -3,20 +3,20 @@
|
||||
"WorkspaceRootPath": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\",
|
||||
"Documents": [
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Set.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Set.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Map.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:RBTree.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
},
|
||||
{
|
||||
"AbsoluteMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||
"RelativeMoniker": "D:0:0:{2827E392-69FE-4ABC-84E6-BF5E25017C39}|Windows_Set_Map.vcxproj|solutionrelative:Main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||
}
|
||||
],
|
||||
"DocumentGroupContainers": [
|
||||
@ -26,7 +26,7 @@
|
||||
"DocumentGroups": [
|
||||
{
|
||||
"DockedWidth": 267,
|
||||
"SelectedChildIndex": 3,
|
||||
"SelectedChildIndex": 6,
|
||||
"Children": [
|
||||
{
|
||||
"$type": "Bookmark",
|
||||
@ -42,20 +42,20 @@
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 0,
|
||||
"DocumentIndex": 3,
|
||||
"Title": "Main.cpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp",
|
||||
"RelativeDocumentMoniker": "Main.cpp",
|
||||
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Main.cpp",
|
||||
"RelativeToolTip": "Main.cpp",
|
||||
"ViewState": "AgIAAF4BAAAAAAAAAAAswGgBAAATAAAAAAAAAA==",
|
||||
"ViewState": "AgIAAHoBAAAAAAAAAAAswIUBAAAUAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
|
||||
"WhenOpened": "2024-09-23T15:54:15.721Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 1,
|
||||
"DocumentIndex": 2,
|
||||
"Title": "RBTree.hpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\RBTree.hpp",
|
||||
"RelativeDocumentMoniker": "RBTree.hpp",
|
||||
@ -68,26 +68,26 @@
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 2,
|
||||
"DocumentIndex": 1,
|
||||
"Title": "Set.hpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Set.hpp",
|
||||
"RelativeDocumentMoniker": "Set.hpp",
|
||||
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Set.hpp",
|
||||
"RelativeToolTip": "Set.hpp",
|
||||
"ViewState": "AgIAAC4AAAAAAAAAAAAswDYAAAAjAAAAAAAAAA==",
|
||||
"ViewState": "AgIAADkAAAAAAAAAAAAswEQAAAAJAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
|
||||
"WhenOpened": "2024-09-23T15:53:53.08Z",
|
||||
"EditorCaption": ""
|
||||
},
|
||||
{
|
||||
"$type": "Document",
|
||||
"DocumentIndex": 3,
|
||||
"DocumentIndex": 0,
|
||||
"Title": "Map.hpp",
|
||||
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp",
|
||||
"RelativeDocumentMoniker": "Map.hpp",
|
||||
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\9_Set_Map\\Windows_Set_Map\\Map.hpp",
|
||||
"RelativeToolTip": "Map.hpp",
|
||||
"ViewState": "AgIAAC4AAAAAAAAAAAAswEEAAAABAAAAAAAAAA==",
|
||||
"ViewState": "AgIAAD4AAAAAAAAAAAAswEYAAAAJAAAAAAAAAA==",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
|
||||
"WhenOpened": "2024-09-23T15:53:43.888Z",
|
||||
"EditorCaption": ""
|
||||
|
BIN
Windows_Set_Map/.vs/Windows_Set_Map/v17/Solution.VC.db
Normal file
BIN
Windows_Set_Map/.vs/Windows_Set_Map/v17/Solution.VC.db
Normal file
Binary file not shown.
Binary file not shown.
@ -354,6 +354,33 @@ void test_Map2()
|
||||
cout << "IsRBTree? " << (countMap.IsRBTree() ? "true" : "false") << endl;
|
||||
}
|
||||
|
||||
void test_Map3()
|
||||
{
|
||||
Lenyiin::Map<int, std::string> map;
|
||||
|
||||
// 插入键值对
|
||||
map.Insert(std::make_pair(1, "one"));
|
||||
map.Insert(std::make_pair(2, "two"));
|
||||
map.Insert(std::make_pair(3, "three"));
|
||||
|
||||
// 通过 operator[] 访问或插入
|
||||
map[4] = "four";
|
||||
std::cout << "Key 4: " << map[4] << std::endl;
|
||||
|
||||
// 查找元素
|
||||
auto it = map.Find(2);
|
||||
if (it != map.end())
|
||||
{
|
||||
std::cout << "Found: " << it->first << " -> " << it->second << std::endl;
|
||||
}
|
||||
|
||||
// 删除元素
|
||||
map.Erase(1);
|
||||
|
||||
// 验证红黑树
|
||||
cout << "IsRBTree? " << (map.IsRBTree() ? "true" : "false") << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// test_set();
|
||||
@ -365,6 +392,7 @@ int main()
|
||||
test_Set();
|
||||
test_Map1();
|
||||
test_Map2();
|
||||
test_Map3();
|
||||
|
||||
return 0;
|
||||
}
|
@ -4,31 +4,37 @@
|
||||
|
||||
namespace Lenyiin
|
||||
{
|
||||
// Map 容器的实现,依赖于红黑树
|
||||
template <class K, class V>
|
||||
class Map
|
||||
{
|
||||
// 用于从节点中获取键值的仿函数
|
||||
struct MapKeyOfT
|
||||
{
|
||||
const K& operator()(const std::pair<K, V>& kv)
|
||||
{
|
||||
return kv.first;
|
||||
return kv.first; // 返回键作为红黑树的比较依据
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
// 定义迭代器类型,支持遍历和访问集合中的元素
|
||||
typedef typename RBTree<K, std::pair<K, V>, MapKeyOfT>::iterator iterator;
|
||||
typedef typename RBTree<K, std::pair<K, V>, MapKeyOfT>::const_iterator const_iterator;
|
||||
|
||||
// 返回指向第一个元素的迭代器
|
||||
iterator begin()
|
||||
{
|
||||
return _tree.begin();
|
||||
}
|
||||
|
||||
// 返回指向尾后位置的迭代器
|
||||
iterator end()
|
||||
{
|
||||
return _tree.end();
|
||||
}
|
||||
|
||||
// const 版本的迭代器
|
||||
const_iterator begin() const
|
||||
{
|
||||
return _tree.begin();
|
||||
@ -39,22 +45,32 @@ namespace Lenyiin
|
||||
return _tree.end();
|
||||
}
|
||||
|
||||
// 插入元素,返回插入结果
|
||||
std::pair<iterator, bool> Insert(const std::pair<K, V>& kv)
|
||||
{
|
||||
return _tree.Insert(kv);
|
||||
}
|
||||
|
||||
// 删除元素,返回是否删除成功
|
||||
bool Erase(const K& key)
|
||||
{
|
||||
return _tree.Erase(key);
|
||||
}
|
||||
|
||||
// 查找元素,返回指向该元素的迭代器
|
||||
iterator Find(const K& key)
|
||||
{
|
||||
return _tree.Find(key);
|
||||
}
|
||||
|
||||
// [] 下标运算符
|
||||
V& operator[](const K& key)
|
||||
{
|
||||
std::pair<iterator, bool> ret = _tree.Insert(std::make_pair(key, V()));
|
||||
return ret.first->second;
|
||||
}
|
||||
|
||||
// 校验红黑树的合法性
|
||||
bool IsRBTree()
|
||||
{
|
||||
return _tree.IsRBTree();
|
||||
|
@ -646,7 +646,7 @@ namespace Lenyiin
|
||||
}
|
||||
|
||||
// 查找节点
|
||||
iterator& Find(const K& key)
|
||||
iterator Find(const K& key)
|
||||
{
|
||||
KOfT koft;
|
||||
Node* cur = _root;
|
||||
|
@ -4,9 +4,11 @@
|
||||
|
||||
namespace Lenyiin
|
||||
{
|
||||
// Set 容器的实现,依赖于红黑树
|
||||
template <class K>
|
||||
class Set
|
||||
{
|
||||
// 用于从节点中获取键值的仿函数
|
||||
struct SetKeyOfT
|
||||
{
|
||||
const K& operator()(const K& k)
|
||||
@ -16,20 +18,24 @@ namespace Lenyiin
|
||||
};
|
||||
|
||||
public:
|
||||
// 定义迭代器类型,支持遍历和访问集合中的元素
|
||||
// 这里RBTree<K, K, SetKeyOfT>::iterator还没有实例化, 系统找不到, typename告诉编译器现在先不着急找
|
||||
typedef typename RBTree<K, K, SetKeyOfT>::iterator iterator;
|
||||
typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;
|
||||
|
||||
// 返回指向第一个元素的迭代器
|
||||
iterator begin()
|
||||
{
|
||||
return _tree.begin();
|
||||
}
|
||||
|
||||
// 返回指向尾后位置的迭代器
|
||||
iterator end()
|
||||
{
|
||||
return _tree.end();
|
||||
}
|
||||
|
||||
// const 版本的迭代器
|
||||
const_iterator begin() const
|
||||
{
|
||||
return _tree.begin();
|
||||
@ -40,27 +46,32 @@ namespace Lenyiin
|
||||
return _tree.end();
|
||||
}
|
||||
|
||||
// 插入元素,返回插入结果
|
||||
std::pair<iterator, bool> Insert(const K& key)
|
||||
{
|
||||
return _tree.Insert(key);
|
||||
}
|
||||
|
||||
// 删除元素,返回是否删除成功
|
||||
bool Erase(const K& key)
|
||||
{
|
||||
return _tree.Erase(key);
|
||||
}
|
||||
|
||||
iterator& Find(const K& key)
|
||||
// 查找元素,返回指向该元素的迭代器
|
||||
iterator Find(const K& key)
|
||||
{
|
||||
return _tree.Find(key);
|
||||
}
|
||||
|
||||
// 校验红黑树的合法性
|
||||
bool IsRBTree()
|
||||
{
|
||||
return _tree.IsRBTree();
|
||||
}
|
||||
|
||||
private:
|
||||
// 底层红黑树实例
|
||||
RBTree<K, K, SetKeyOfT> _tree;
|
||||
};
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user