From 706cb1990fdd99077a0e06b5b09d04dde028cd8e Mon Sep 17 00:00:00 2001 From: ultraman <1394466835@qq.com> Date: Fri, 24 May 2024 09:04:02 +0800 Subject: [PATCH] feat: update --- 2024/05/cpp_hashmap.md | 21 +++++++++++++++++++++ README.md | 1 + 2 files changed, 22 insertions(+) create mode 100644 2024/05/cpp_hashmap.md diff --git a/2024/05/cpp_hashmap.md b/2024/05/cpp_hashmap.md new file mode 100644 index 0000000..3930d3b --- /dev/null +++ b/2024/05/cpp_hashmap.md @@ -0,0 +1,21 @@ +[Code](https://github.com/cs-learning-every-day/CS106L/tree/main/cs106L-assignment2) + +主要是const和template的使用结合。 + +const函数都去直接掉用非const函数 + +```sh +template +typename HashMap::const_iterator HashMap::begin() const { + // This is called the static_cast/const_cast trick, which allows us to reuse + // the non-const version of find to implement the const version. + // The idea is to cast this so it's pointing to a non-const HashMap, which + // calls the overload above (and prevent infinite recursion). + // Also note that we are calling the conversion operator in the iterator + // class! + return static_cast( + const_cast*>(this)->begin()); +} +``` + +底层就是一个bucket array(hash索引), bucket是一个链表存储索引冲突的节点。 diff --git a/README.md b/README.md index ecd2b2a..d465281 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ The best thing to do is have fun. - [Hobby Projects](/hobby_projects.md) - [Kata](/kata.md) - **2024-05** + - [怎么实现一个HashMap STL-compliant, industrial strength, robust, and blazingly fast data structure](/2024/05/cpp_hashmap.md) - [RC-EaseProbe 一个优雅的状态检测工具,支持多种协议和通知平台](/2024/05/rc_easeprobe.md) - [从一个Wiki地址如何一步一步的到另一个Wiki地址](/2024/05/wiki_start_to_wiki_end.md) - [如何手搓一个 CPU?](/2024/05/cs61cpu.md)