From 42950ef3ffa98ced73f0745d2fcdd036b343f8a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=BE=9C=E5=8D=9A?= <2398228137@qq.com> Date: Wed, 8 Jan 2025 21:13:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'src/MyTinySTL-master/MyTinySTL'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 我是赵澜博 --- .../MyTinySTL/unordered_set.h | 1437 +++++++++-------- 1 file changed, 750 insertions(+), 687 deletions(-) diff --git a/src/MyTinySTL-master/MyTinySTL/unordered_set.h b/src/MyTinySTL-master/MyTinySTL/unordered_set.h index f4dba1b..7599c64 100644 --- a/src/MyTinySTL-master/MyTinySTL/unordered_set.h +++ b/src/MyTinySTL-master/MyTinySTL/unordered_set.h @@ -1,687 +1,750 @@ -namespace mystl -{ - - // 模板类 unordered_set,用于存储不允许重复的键值。 - // 参数一代表键值类型,参数二代表哈希函数,默认使用 mystl::hash, - // 参数三代表键值比较方式,默认使用 mystl::equal_to。 - template , class KeyEqual = mystl::equal_to> - class unordered_set - { - private: - // 使用 hashtable 作为底层机制来存储数据。 - typedef hashtable base_type; - base_type ht_; // 实际存储数据的哈希表对象 - - public: - // 使用 hashtable 的型别定义 - typedef typename base_type::allocator_type allocator_type; // 分配器类型 - typedef typename base_type::key_type key_type; // 键类型 - typedef typename base_type::value_type value_type; // 值类型 - typedef typename base_type::hasher hasher; // 哈希函数类型 - typedef typename base_type::key_equal key_equal; // 键值比较函数类型 - - typedef typename base_type::size_type size_type; // 大小类型 - typedef typename base_type::difference_type difference_type; // 差异类型 - typedef typename base_type::pointer pointer; // 指针类型 - typedef typename base_type::const_pointer const_pointer; // 常量指针类型 - typedef typename base_type::reference reference; // 引用类型 - typedef typename base_type::const_reference const_reference; // 常量引用类型 - - typedef typename base_type::const_iterator iterator; // 迭代器类型 - typedef typename base_type::const_iterator const_iterator; // 常量迭代器类型 - typedef typename base_type::const_local_iterator local_iterator; // 本地迭代器类型 - typedef typename base_type::const_local_iterator const_local_iterator; // 常量本地迭代器类型 - - // 获取分配器 - allocator_type get_allocator() const { return ht_.get_allocator(); } - - public: - // 构造、复制、移动函数 - - // 默认构造函数,初始化哈希表为默认大小和哈希函数 - unordered_set() - :ht_(100, Hash(), KeyEqual()) - { - } - - // 自定义构造函数,允许指定桶的数量、哈希函数和键值比较函数 - explicit unordered_set(size_type bucket_count, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(bucket_count, hash, equal) - { - } - - // 范围构造函数,从输入迭代器范围中初始化 - template - unordered_set(InputIterator first, InputIterator last, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - : ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) - { - for (; first != last; ++first) - ht_.insert_unique_noresize(*first); // 插入元素,不重新分配空间 - } - - // 列表初始化构造函数 - unordered_set(std::initializer_list ilist, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(mystl::max(bucket_count, static_cast(ilist.size())), hash, equal) - { - for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) - ht_.insert_unique_noresize(*first); // 插入元素,不重新分配空间 - } - - // 复制构造函数 - unordered_set(const unordered_set& rhs) - :ht_(rhs.ht_) - { - } - - // 移动构造函数 - unordered_set(unordered_set&& rhs) noexcept - : ht_(mystl::move(rhs.ht_)) - { - } - - // 复制赋值运算符 - unordered_set& operator=(const unordered_set& rhs) - { - ht_ = rhs.ht_; - return *this; - } - - // 移动赋值运算符 - unordered_set& operator=(unordered_set&& rhs) - { - ht_ = mystl::move(rhs.ht_); - return *this; - } - - // 列表赋值运算符 - unordered_set& operator=(std::initializer_list ilist) - { - ht_.clear(); - ht_.reserve(ilist.size()); - for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) - ht_.insert_unique_noresize(*first); // 插入元素,不重新分配空间 - return *this; - } - - // 析构函数 - ~unordered_set() = default; - - // 迭代器相关函数 - - // 返回指向容器开始的迭代器 - iterator begin() noexcept { return ht_.begin(); } - const_iterator begin() const noexcept { return ht_.begin(); } - - // 返回指向容器结束的迭代器 - iterator end() noexcept { return ht_.end(); } - const_iterator end() const noexcept { return ht_.end(); } - - // 返回指向容器开始的常量迭代器 - const_iterator cbegin() const noexcept { return ht_.cbegin(); } - const_iterator cend() const noexcept { return ht_.cend(); } - - // 容量相关函数 - - // 判断容器是否为空 - bool empty() const noexcept { return ht_.empty(); } - // 返回容器中元素的数量 - size_type size() const noexcept { return ht_.size(); } - // 返回容器可以容纳的最大元素数量 - size_type max_size() const noexcept { return ht_.max_size(); } - }; - - // 修改容器操作 - - // emplace / emplace_hint - // emplace 函数用于在容器中直接构造一个新元素,参数是构造函数需要的参数。 - // emplace_hint 函数与 emplace 类似,但提供了一个迭代器 hint 作为插入位置的提示。 - template - pair emplace(Args&& ...args) - { - // 调用底层哈希表的 emplace_unique 函数,尝试插入一个新元素。 - // mystl::forward 用于完美转发参数,保留参数的引用类型。 - return ht_.emplace_unique(mystl::forward(args)...); - } - - template - iterator emplace_hint(const_iterator hint, Args&& ...args) - { - // 调用底层哈希表的 emplace_unique_use_hint 函数,尝试插入一个新元素。 - // hint 作为插入位置的提示,可以提高插入效率。 - return ht_.emplace_unique_use_hint(hint, mystl::forward(args)...); - } - - // insert - // insert 函数用于将一个元素插入容器中,如果元素已存在则不插入。 - pair insert(const value_type& value) - { - // 调用底层哈希表的 insert_unique 函数,尝试插入一个新元素。 - return ht_.insert_unique(value); - } - - pair insert(value_type&& value) - { - // 调用底层哈希表的 emplace_unique 函数,尝试插入一个新元素。 - // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制。 - return ht_.emplace_unique(mystl::move(value)); - } - - iterator insert(const_iterator hint, const value_type& value) - { - // 调用底层哈希表的 insert_unique_use_hint 函数,尝试插入一个新元素。 - // hint 作为插入位置的提示,可以提高插入效率。 - return ht_.insert_unique_use_hint(hint, value); - } - - iterator insert(const_iterator hint, value_type&& value) - { - // 调用底层哈希表的 emplace_unique_use_hint 函数,尝试插入一个新元素。 - // hint 作为插入位置的提示,可以提高插入效率。 - // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制。 - return ht_.emplace_unique_use_hint(hint, mystl::move(value)); - } - - template - void insert(InputIterator first, InputIterator last) - { - // 调用底层哈希表的 insert_unique 函数,将一个范围内的元素插入容器中。 - ht_.insert_unique(first, last); - } - - // erase / clear - // erase 函数用于从容器中删除元素。 - void erase(iterator it) - { - // 调用底层哈希表的 erase 函数,删除指定位置的元素。 - ht_.erase(it); - } - - void erase(iterator first, iterator last) - { - // 调用底层哈希表的 erase 函数,删除指定范围内的元素。 - ht_.erase(first, last); - } - - size_type erase(const key_type& key) - { - // 调用底层哈希表的 erase_unique 函数,删除指定键值的元素。 - return ht_.erase_unique(key); - } - - void clear() - { - // 调用底层哈希表的 clear 函数,清空容器中的所有元素。 - ht_.clear(); - } - - void swap(unordered_set& other) noexcept - { - // 调用底层哈希表的 swap 函数,交换两个容器的内容。 - ht_.swap(other.ht_); - } - - // 查找相关 - // count 函数用于返回指定键值的元素个数。 - size_type count(const key_type& key) const - { - // 调用底层哈希表的 count 函数,返回指定键值的元素个数。 - return ht_.count(key); - } - - // find 函数用于查找指定键值的元素,并返回指向该元素的迭代器。 - iterator find(const key_type& key) - { - // 调用底层哈希表的 find 函数,查找指定键值的元素。 - return ht_.find(key); - } - - const_iterator find(const key_type& key) const - { - // 调用底层哈希表的 find 函数,查找指定键值的元素。 - return ht_.find(key); - } - - // equal_range 函数用于查找指定键值的元素范围,并返回一个迭代器对。 - pair equal_range(const key_type& key) - { - // 调用底层哈希表的 equal_range_unique 函数,查找指定键值的元素范围。 - return ht_.equal_range_unique(key); - } - - pair equal_range(const key_type& key) const - { - // 调用底层哈希表的 equal_range_unique 函数,查找指定键值的元素范围。 - return ht_.equal_range_unique(key); - } - - // bucket interface - // begin 函数用于返回指向指定桶的开始的迭代器。 - local_iterator begin(size_type n) noexcept - { - // 调用底层哈希表的 begin 函数,返回指向指定桶的开始的迭代器。 - return ht_.begin(n); - } - - const_local_iterator begin(size_type n) const noexcept - { - // 调用底层哈希表的 begin 函数,返回指向指定桶的开始的迭代器。 - return ht_.begin(n); - } - - const_local_iterator cbegin(size_type n) const noexcept - { - // 调用底层哈希表的 cbegin 函数,返回指向指定桶的开始的常量迭代器。 - return ht_.cbegin(n); - } - - // end 函数用于返回指向指定桶的结束的迭代器。 - local_iterator end(size_type n) noexcept - { - // 调用底层哈希表的 end 函数,返回指向指定桶的结束的迭代器。 - return ht_.end(n); - } - - const_local_iterator end(size_type n) const noexcept - { - // 调用底层哈希表的 end 函数,返回指向指定桶的结束的迭代器。 - return ht_.end(n); - } - - const_local_iterator cend(size_type n) const noexcept - { - // 调用底层哈希表的 cend 函数,返回指向指定桶的结束的常量迭代器。 - return ht_.cend(n); - } - - // bucket_count 函数用于返回容器中的桶数量。 - size_type bucket_count() const noexcept - { - // 调用底层哈希表的 bucket_count 函数,返回容器中的桶数量。 - return ht_.bucket_count(); - } - - // max_bucket_count 函数用于返回容器中允许的最大桶数量。 - size_type max_bucket_count() const noexcept - { - // 调用底层哈希表的 max_bucket_count 函数,返回容器中允许的最大桶数量。 - return ht_.max_bucket_count(); - } - - // bucket_size 函数用于返回指定桶中的元素数量。 - size_type bucket_size(size_type n) const noexcept - { - // 调用底层哈希表的 bucket_size 函数,返回指定桶中的元素数量。 - return ht_.bucket_size(n); - } - - // bucket 函数用于返回指定键值所在的桶的索引。 - size_type bucket(const key_type& key) const - { - // 调用底层哈希表的 bucket 函数,返回指定键值所在的桶的索引。 - return ht_.bucket(key); - } - - // hash policy - // load_factor 函数用于返回容器的负载因子,即元素数量与桶数量的比值。 - float load_factor() const noexcept { return ht_.load_factor(); } - - // max_load_factor 函数用于获取或设置容器的最大负载因子。 - float max_load_factor() const noexcept { return ht_.max_load_factor(); } - void max_load_factor(float ml) { ht_.max_load_factor(ml); } - - // rehash 函数用于重新哈希容器,指定新的桶数量。 - void rehash(size_type count) { ht_.rehash(count); } - - // reserve 函数用于预留空间,指定容器需要容纳的元素数量。 - void reserve(size_type count) { ht_.reserve(count); } - - // hash_fcn 函数用于返回容器使用的哈希函数。 - hasher hash_fcn() const { return ht_.hash_fcn(); } - - // key_eq 函数用于返回容器使用的键值比较函数。 - key_equal key_eq() const { return ht_.key_eq(); } - -public: - // 重载相等操作符 - friend bool operator==(const unordered_set& lhs, const unordered_set& rhs) - { - // 调用底层哈希表的 equal_range_unique 函数,比较两个容器的元素是否相等。 - return lhs.ht_.equal_range_unique(rhs.ht_); - } - - // 重载不等操作符 - friend bool operator!=(const unordered_set& lhs, const unordered_set& rhs) - { - // 调用底层哈希表的 equal - -/*****************************************************************************************/ -// 模板类 unordered_multiset,用于存储允许重复的键值。 -// 参数一代表键值类型,参数二代表哈希函数,默认使用 mystl::hash, -// 参数三代表键值比较方式,默认使用 mystl::equal_to。 - template , class KeyEqual = mystl::equal_to> - class unordered_multiset - { - private: - // 使用 hashtable 作为底层机制来存储数据。 - typedef hashtable base_type; - base_type ht_; // 实际存储数据的哈希表对象 - - public: - // 使用 hashtable 的型别定义 - typedef typename base_type::allocator_type allocator_type; // 分配器类型 - typedef typename base_type::key_type key_type; // 键类型 - typedef typename base_type::value_type value_type; // 值类型 - typedef typename base_type::hasher hasher; // 哈希函数类型 - typedef typename base_type::key_equal key_equal; // 键值比较函数类型 - - typedef typename base_type::size_type size_type; // 大小类型 - typedef typename base_type::difference_type difference_type; // 差异类型 - typedef typename base_type::pointer pointer; // 指针类型 - typedef typename base_type::const_pointer const_pointer; // 常量指针类型 - typedef typename base_type::reference reference; // 引用类型 - typedef typename base_type::const_reference const_reference; // 常量引用类型 - - typedef typename base_type::const_iterator iterator; // 迭代器类型 - typedef typename base_type::const_iterator const_iterator; // 常量迭代器类型 - typedef typename base_type::const_local_iterator local_iterator; // 本地迭代器类型 - typedef typename base_type::const_local_iterator const_local_iterator; // 常量本地迭代器类型 - - // 获取分配器 - allocator_type get_allocator() const { return ht_.get_allocator(); } - - public: - // 构造、复制、移动函数 - - // 默认构造函数,初始化哈希表为默认大小和哈希函数 - unordered_multiset() - :ht_(100, Hash(), KeyEqual()) - { - } - - // 自定义构造函数,允许指定桶的数量、哈希函数和键值比较函数 - explicit unordered_multiset(size_type bucket_count, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(bucket_count, hash, equal) - { - } - - // 范围构造函数,从输入迭代器范围中初始化 - template - unordered_multiset(InputIterator first, InputIterator last, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - : ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) - { - for (; first != last; ++first) - ht_.insert_multi_noresize(*first); // 插入元素,不重新分配空间 - } - - // 列表初始化构造函数 - unordered_multiset(std::initializer_list ilist, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(mystl::max(bucket_count, static_cast(ilist.size())), hash, equal) - { - for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) - ht_.insert_multi_noresize(*first); // 插入元素,不重新分配空间 - } - - // 复制构造函数 - unordered_multiset(const unordered_multiset& rhs) - :ht_(rhs.ht_) - { - } - - // 移动构造函数 - unordered_multiset(unordered_multiset&& rhs) noexcept - : ht_(mystl::move(rhs.ht_)) - { - } - - // 复制赋值运算符 - unordered_multiset& operator=(const unordered_multiset& rhs) - { - ht_ = rhs.ht_; - return *this; - } - - // 移动赋值运算符 - unordered_multiset& operator=(unordered_multiset&& rhs) - { - ht_ = mystl::move(rhs.ht_); - return *this; - } - - // 列表赋值运算符 - unordered_multiset& operator=(std::initializer_list ilist) - { - ht_.clear(); - ht_.reserve(ilist.size()); - for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) - ht_.insert_multi_noresize(*first); // 插入元素,不重新分配空间 - return *this; - } - - // 析构函数 - ~unordered_multiset() = default; - - // 迭代器相关函数 - - // 返回指向容器开始的迭代器 - iterator begin() noexcept { return ht_.begin(); } - const_iterator begin() const noexcept { return ht_.begin(); } - - // 返回指向容器结束的迭代器 - iterator end() noexcept { return ht_.end(); } - const_iterator end() const noexcept { return ht_.end(); } - - // 返回指向容器开始的常量迭代器 - const_iterator cbegin() const noexcept { return ht_.cbegin(); } - const_iterator cend() const noexcept { return ht_.cend(); } - - // 容量相关函数 - - // 判断容器是否为空 - bool empty() const noexcept { return ht_.empty(); } - // 返回容器中元素的数量 - size_type size() const noexcept { return ht_.size(); } - // 返回容器可以容纳的最大元素数量 - size_type max_size() const noexcept { return ht_.max_size(); } - }; - // 修改容器相关 - - // emplace / emplace_hint - // emplace 函数用于在容器中直接构造一个新元素,参数是构造函数需要的参数。 - // emplace_hint 函数与 emplace 类似,但提供了一个迭代器 hint 作为插入位置的提示。 - template - iterator emplace(Args&& ...args) - { - // 调用底层哈希表的 emplace_multi 函数,尝试插入一个新元素。 - // mystl::forward 用于完美转发参数,保留参数的引用类型。 - return ht_.emplace_multi(mystl::forward(args)...); - } - - template - iterator emplace_hint(const_iterator hint, Args&& ...args) - { - // 调用底层哈希表的 emplace_multi_use_hint 函数,尝试插入一个新元素。 - // hint 作为插入位置的提示,可以提高插入效率。 - return ht_.emplace_multi_use_hint(hint, mystl::forward(args)...); - } - - // insert - // insert 函数用于将一个元素插入容器中,允许重复。 - iterator insert(const value_type& value) - { - // 调用底层哈希表的 insert_multi 函数,插入一个新元素。 - return ht_.insert_multi(value); - } - - iterator insert(value_type&& value) - { - // 调用底层哈希表的 emplace_multi 函数,插入一个新元素。 - // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制。 - return ht_.emplace_multi(mystl::move(value)); - } - - iterator insert(const_iterator hint, const value_type& value) - { - // 调用底层哈希表的 insert_multi_use_hint 函数,插入一个新元素。 - // hint 作为插入位置的提示,可以提高插入效率。 - return ht_.insert_multi_use_hint(hint, value); - } - - iterator insert(const_iterator hint, value_type&& value) - { - // 调用底层哈希表的 emplace_multi_use_hint 函数,插入一个新元素。 - // hint 作为插入位置的提示,可以提高插入效率。 - // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制。 - return ht_.emplace_multi_use_hint(hint, mystl::move(value)); - } - - template - void insert(InputIterator first, InputIterator last) - { - // 调用底层哈希表的 insert_multi 函数,将一个范围内的元素插入容器中。 - ht_.insert_multi(first, last); - } - - // erase / clear - // erase 函数用于从容器中删除元素。 - void erase(iterator it) - { - // 调用底层哈希表的 erase 函数,删除指定位置的元素。 - ht_.erase(it); - } - - void erase(iterator first, iterator last) - { - // 调用底层哈希表的 erase 函数,删除指定范围内的元素。 - ht_.erase(first, last); - } - - size_type erase(const key_type& key) - { - // 调用底层哈希表的 erase_multi 函数,删除指定键值的所有元素。 - return ht_.erase_multi(key); - } - - void clear() - { - // 调用底层哈希表的 clear 函数,清空容器中的所有元素。 - ht_.clear(); - } - - void swap(unordered_multiset& other) noexcept - { - // 调用底层哈希表的 swap 函数,交换两个容器的内容。 - ht_.swap(other.ht_); - } - - // 查找相关 - // count 函数用于返回指定键值的元素个数。 - size_type count(const key_type& key) const - { - // 调用底层哈希表的 count 函数,返回指定键值的元素个数。 - return ht_.count(key); - } - - // find 函数用于查找指定键值的元素,并返回指向该元素的迭代器。 - iterator find(const key_type& key) - { - // 调用底层哈希表的 find 函数,查找指定键值的元素。 - return ht_.find(key); - } - - const_iterator find(const key_type& key) const - { - // 调用底层哈希表的 find 函数,查找指定键值的元素。 - return ht_.find(key); - } - - // equal_range 函数用于查找指定键值的元素范围,并返回一个迭代器对。 - pair equal_range(const key_type& key) - { - // 调用底层哈希表的 equal_range_multi 函数,查找指定键值的元素范围。 - return ht_.equal_range_multi(key); - } - - pair equal_range(const key_type& key) const - { - // 调用底层哈希表的 equal_range_multi 函数,查找指定键值的元素范围。 - return ht_.equal_range_multi(key); - } - - // bucket interface - // begin 函数用于返回指向指定桶的开始的迭代器。 - local_iterator begin(size_type n) noexcept - { - // 调用底层哈希表的 begin 函数,返回指向指定桶的开始的迭代器。 - return ht_.begin(n); - } - - const_local_iterator begin(size_type n) const noexcept - { - // 调用底层哈希表的 begin 函数,返回指向指定桶的开始的迭代器。 - return ht_.begin(n); - } - - const_local_iterator cbegin(size_type n) const noexcept - { - // 调用底层哈希表的 cbegin 函数,返回指向指定桶的开始的常量迭代器。 - return ht_.cbegin(n); - } - - // end 函数用于返回指向指定桶的结束的迭代器。 - local_iterator end(size_type n) noexcept - { - // 调用底层哈希表的 end 函数,返回指向指定桶的结束的迭代器。 - return ht_.end(n); - } - - const_local_iterator end(size_type n) const noexcept - { - // 调用底层哈希表的 end 函数,返回指向指定桶的结束的迭代器。 - return ht_.end(n); - } - - const_local_iterator cend(size_type n) const noexcept - { - // 调用底层哈希表的 cend 函数,返回指向指定桶的结束的常量迭代器。 - return ht_.cend(n); - } - - // bucket_count 函数用于返回容器中的桶数量。 - size_type bucket_count() const noexcept - { - // 调用底层哈希表的 bucket_count 函数,返回容器中的桶数量。 - return ht_.bucket_count(); - } - - // max_bucket_count 函数用于返回容器中允许的最大桶数量。 - size_type max_bucket_count() const noexcept - { - // 调用底层哈希表的 max_bucket_count 函数,返回容器中允许的最大桶数量。 - return ht_.max_bucket_count(); - } - - // bucket_size 函数用于返回指定桶中的元素数量。 - size_type bucket_size(size_type n) const noexcept - { - // 调用底层哈希表的 bucket_size 函数,返回指定桶中的元素数量。 - return ht_.bucket_size(n); - } - - // bucket 函数用于返回指定键值所在的桶的索引。 - size_type bucket(const ke \ No newline at end of file +/*这段代码定义了两个模板类 unordered_set 和 unordered_multiset,它们都是基于哈希表(hashtable)实现的关联容器,用于存储键值。以下是这两个类的主要功能和区别: + +unordered_set 功能 +存储不允许重复的键值:unordered_set 用于存储唯一的键值,不允许键值重复。 +哈希表底层实现:使用哈希表作为底层数据结构,提供高效的插入、删除和查找操作。 +迭代器支持:提供迭代器和常量迭代器,用于遍历容器中的元素。 +容量操作:可以检查容器是否为空、获取容器的大小和最大容量。 +元素操作: +插入:通过 emplace 和 insert 方法插入元素,如果元素已存在则不插入。 +删除:通过 erase 方法删除指定元素或范围内的元素。 +清空:通过 clear 方法清空容器中的所有元素。 +查找操作:提供 find 和 equal_range 方法用于查找元素。 +桶操作:提供桶相关的操作,如获取桶的数量、桶的大小和元素所在的桶编号。 +哈希策略:可以获取和设置负载因子、重新哈希和预留空间。 +比较操作:重载了 == 和 != 操作符,用于比较两个 unordered_set 是否相等或不相等。 +unordered_multiset 功能 +存储允许重复的键值:与 unordered_set 不同,unordered_multiset 允许键值重复。 +哈希表底层实现:同样使用哈希表作为底层数据结构,提供高效的插入、删除和查找操作。 +迭代器支持:提供迭代器和常量迭代器,用于遍历容器中的元素。 +容量操作:可以检查容器是否为空、获取容器的大小和最大容量。 +元素操作: +插入:通过 emplace 和 insert 方法插入元素,允许键值重复。 +删除:通过 erase 方法删除指定元素或范围内的元素。 +清空:通过 clear 方法清空容器中的所有元素。 +查找操作:提供 find 和 equal_range 方法用于查找元素。 +桶操作:提供桶相关的操作,如获取桶的数量、桶的大小和元素所在的桶编号。 +哈希策略:可以获取和设置负载因子、重新哈希和预留空间。 +总结 +unordered_set 适用于需要存储唯一键值的场景,例如存储不重复的标识符或集合。 +unordered_multiset 适用于需要存储允许重复键值的场景,例如统计元素出现的次数或处理多值集合。 +这两个类提供了丰富的操作和灵活的使用方式,是实现高效关联容器的有力工具。*/ +namespace mystl // 定义命名空间 mystl,用于封装自定义的 STL 组件 +{ + // 模板类 unordered_set,用于存储不允许重复的键值 + // 参数一代表键值类型,参数二代表哈希函数,默认使用 mystl::hash, + // 参数三代表键值比较方式,默认使用 mystl::equal_to。 + template , class KeyEqual = mystl::equal_to> + class unordered_set + { + private: + // 使用 hashtable 作为底层机制来存储数据 + typedef hashtable base_type; + base_type ht_; // 实际存储数据的哈希表对象 + + public: + // 使用 hashtable 的型别定义 + typedef typename base_type::allocator_type allocator_type; // 分配器类型 + typedef typename base_type::key_type key_type; // 键类型 + typedef typename base_type::value_type value_type; // 值类型 + typedef typename base_type::hasher hasher; // 哈希函数类型 + typedef typename base_type::key_equal key_equal; // 键值比较函数类型 + typedef typename base_type::size_type size_type; // 大小类型 + typedef typename base_type::difference_type difference_type; // 差异类型 + typedef typename base_type::pointer pointer; // 指针类型 + typedef typename base_type::const_pointer const_pointer; // 常量指针类型 + typedef typename base_type::reference reference; // 引用类型 + typedef typename base_type::const_reference const_reference; // 常量引用类型 + typedef typename base_type::iterator iterator; // 迭代器类型 + typedef typename base_type::const_iterator const_iterator; // 常量迭代器类型 + typedef typename base_type::local_iterator local_iterator; // 本地迭代器类型 + typedef typename base_type::const_local_iterator const_local_iterator; // 常量本地迭代器类型 + + // 获取分配器 + allocator_type get_allocator() const { return ht_.get_allocator(); } + + public: + // 构造、复制、移动函数 + + // 默认构造函数,初始化哈希表为默认大小和哈希函数 + unordered_set() + :ht_(100, Hash(), KeyEqual()) + { + } + + // 自定义构造函数,允许指定桶的数量、哈希函数和键值比较函数 + explicit unordered_set(size_type bucket_count, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + :ht_(bucket_count, hash, equal) + { + } + + // 范围构造函数,从输入迭代器范围中初始化 + template + unordered_set(InputIterator first, InputIterator last, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + : ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) + { + for (; first != last; ++first) + ht_.insert_unique_noresize(*first); // 插入元素,不重新分配空间 + } + + // 列表初始化构造函数 + unordered_set(std::initializer_list ilist, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + :ht_(mystl::max(bucket_count, static_cast(ilist.size())), hash, equal) + { + for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) + ht_.insert_unique_noresize(*first); // 插入元素,不重新分配空间 + } + + // 复制构造函数 + unordered_set(const unordered_set& rhs) + :ht_(rhs.ht_) + { + } + + // 移动构造函数 + unordered_set(unordered_set&& rhs) noexcept + : ht_(mystl::move(rhs.ht_)) + { + } + + // 复制赋值运算符 + unordered_set& operator=(const unordered_set& rhs) + { + ht_ = rhs.ht_; + return *this; + } + + // 移动赋值运算符 + unordered_set& operator=(unordered_set&& rhs) + { + ht_ = mystl::move(rhs.ht_); + return *this; + } + + // 列表赋值运算符 + unordered_set& operator=(std::initializer_list ilist) + { + ht_.clear(); + ht_.reserve(ilist.size()); + for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) + ht_.insert_unique_noresize(*first); // 插入元素,不重新分配空间 + return *this; + } + + // 析构函数 + ~unordered_set() = default; + + // 迭代器相关函数 + + // 返回指向容器开始的迭代器 + iterator begin() noexcept { return ht_.begin(); } + const_iterator begin() const noexcept { return ht_.begin(); } + + // 返回指向容器结束的迭代器 + iterator end() noexcept { return ht_.end(); } + const_iterator end() const noexcept { return ht_.end(); } + + // 返回指向容器开始的常量迭代器 + const_iterator cbegin() const noexcept { return ht_.cbegin(); } + const_iterator cend() const noexcept { return ht_.cend(); } + + // 容量相关函数 + + // 判断容器是否为空 + bool empty() const noexcept { return ht_.empty(); } + // 返回容器中元素的数量 + size_type size() const noexcept { return ht_.size(); } + // 返回容器可以容纳的最大元素数量 + size_type max_size() const noexcept { return ht_.max_size(); } + }; + + // 修改容器操作 + + // emplace / emplace_hint + // emplace 函数用于在容器中直接构造一个新元素,参数是构造函数需要的参数 + // emplace_hint 函数与 emplace 类似,但提供了一个迭代器 hint 作为插入位置的提示 + template + pair emplace(Args&& ...args) + { + // 调用底层哈希表的 emplace_unique 函数,尝试插入一个新元素 + // mystl::forward 用于完美转发参数,保留参数的引用类型 + return ht_.emplace_unique(mystl::forward(args)...); + } + + template + iterator emplace_hint(const_iterator hint, Args&& ...args) + { + // 调用底层哈希表的 emplace_unique_use_hint 函数,尝试插入一个新元素 + // hint 作为插入位置的提示,可以提高插入效率 + return ht_.emplace_unique_use_hint(hint, mystl::forward(args)...); + } + + // insert + // insert 函数用于将一个元素插入容器中,如果元素已存在则不插入 + pair insert(const value_type& value) + { + // 调用底层哈希表的 insert_unique 函数,尝试插入一个新元素 + return ht_.insert_unique(value); + } + + pair insert(value_type&& value) + { + // 调用底层哈希表的 emplace_unique 函数,尝试插入一个新元素 + // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制 + return ht_.emplace_unique(mystl::move(value)); + } + + iterator insert(const_iterator hint, const value_type& value) + { + // 调用底层哈希表的 insert_unique_use_hint 函数,尝试插入一个新元素 + // hint 作为插入位置的提示,可以提高插入效率 + return ht_.insert_unique_use_hint(hint, value); + } + + iterator insert(const_iterator hint, value_type&& value) + { + // 调用底层哈希表的 emplace_unique_use_hint 函数,尝试插入一个新元素 + // hint 作为插入位置的提示,可以提高插入效率 + // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制 + return ht_.emplace_unique_use_hint(hint, mystl::move(value)); + } + + template + void insert(InputIterator first, InputIterator last) + { + // 调用底层哈希表的 insert_unique 函数,将一个范围内的元素插入容器中 + ht_.insert_unique(first, last); + } + + // erase / clear + // erase 函数用于从容器中删除元素 + void erase(iterator it) + { + ht_.erase(it); // 调用底层哈希表的 erase 函数,删除指定迭代器位置的元素 + } + erase(iterator first, iterator last) + { + ht_.erase(first, last); // 调用底层哈希表的 erase 函数,删除指定范围的元素 + } + + // 删除指定键的所有元素,并返回被删除的元素数量 + size_type erase(const key_type& key) + { + return ht_.erase_unique(key); // 调用底层哈希表的 erase_unique 函数,删除指定键的所有元素 + } + + // 清空容器 + void clear() + { + ht_.clear(); // 调用底层哈希表的 clear 函数,清空容器中的所有元素 + } + + ```cpp +template +void insert(InputIterator first, InputIterator last) +{ + // 将迭代器范围内的元素插入到容器中,利用底层哈希表的插入功能 + ht_.insert_unique(first, last); +} + +// 删除指定迭代器位置的元素 +void erase(iterator it) +{ + // 调用底层哈希表的 erase 方法,移除指定位置的元素 + ht_.erase(it); +} + +// 删除指定范围内的元素 +void erase(iterator first, iterator last) +{ + // 调用底层哈希表的 erase 方法,移除指定范围内的元素 + ht_.erase(first, last); +} + +// 删除所有指定键的元素,并返回删除的元素数量 +size_type erase(const key_type& key) +{ + // 调用底层哈希表的 erase_unique 方法,移除所有指定键的元素 + return ht_.erase_unique(key); +} + +// 清空容器中的所有元素 +void clear() +{ + // 调用底层哈希表的 clear 方法,清空容器 + ht_.clear(); +} + +// 交换两个 unordered_set 容器的内容 +void swap(unordered_set& other) noexcept +{ + // 调用底层哈希表的 swap 方法,交换两个容器的内容 + ht_.swap(other.ht_); +} + +// 返回指定键值的元素个数 +size_type count(const key_type& key) const +{ + // 调用底层哈希表的 count 方法,统计指定键值的元素个数 + return ht_.count(key); +} + +// 查找指定键值的元素,并返回指向该元素的迭代器 +iterator find(const key_type& key) +{ + // 调用底层哈希表的 find 方法,查找指定键值的元素 + return ht_.find(key); +} + +// 查找指定键值的元素,并返回指向该元素的常量迭代器 +const_iterator find(const key_type& key) const +{ + // 调用底层哈希表的 find 方法,查找指定键值的元素 + return ht_.find(key); +} + +// 查找指定键值的元素范围,并返回一个迭代器对 +pair equal_range(const key_type& key) +{ + // 调用底层哈希表的 equal_range_unique 方法,查找指定键值的元素范围 + return ht_.equal_range_unique(key); +} + +// 查找指定键值的元素范围,并返回一个常量迭代器对 +pair equal_range(const key_type& key) const +{ + // 调用底层哈希表的 equal_range_unique 方法,查找指定键值的元素范围 + return ht_.equal_range_unique(key); +} + +// 返回指向指定桶开始的迭代器 +local_iterator begin(size_type n) noexcept +{ + // 调用底层哈希表的 begin 方法,获取指定桶的开始迭代器 + return ht_.begin(n); +} + +// 返回指向指定桶开始的常量迭代器 +const_local_iterator begin(size_type n) const noexcept +{ + // 调用底层哈希表的 begin 方法,获取指定桶的开始常量迭代器 + return ht_.begin(n); +} + +// 返回指向指定桶开始的常量迭代器 +const_local_iterator cbegin(size_type n) const noexcept +{ + // 调用底层哈希表的 cbegin 方法,获取指定桶的开始常量迭代器 + return ht_.cbegin(n); +} + +// 返回指向指定桶结束的迭代器 +local_iterator end(size_type n) noexcept +{ + // 调用底层哈希表的 end 方法,获取指定桶的结束迭代器 + return ht_.end(n); +} + +// 返回指向指定桶结束的常量迭代器 +const_local_iterator end(size_type n) const noexcept +{ + // 调用底层哈希表的 end 方法,获取指定桶的结束常量迭代器 + return ht_.end(n); +} + +// 返回指向指定桶结束的常量迭代器 +const_local_iterator cend(size_type n) const noexcept +{ + // 调用底层哈希表的 cend 方法,获取指定桶的结束常量迭代器 + return ht_.cend(n); +} + +// 返回容器中的桶数量 +size_type bucket_count() const noexcept +{ + // 调用底层哈希表的 bucket_count 方法,获取桶的数量 + return ht_.bucket_count(); +} + +// 返回容器中允许的最大桶数量 +size_type max_bucket_count() const noexcept +{ + // 调用底层哈希表的 max_bucket_count 方法,获取最大桶数量 + return ht_.max_bucket_count(); +} + +// 返回指定桶中的元素数量 +size_type bucket_size(size_type n) const noexcept +{ + // 调用底层哈希表的 bucket_size 方法,获取指定桶的元素数量 + return ht_.bucket_size(n); +} + +// 返回指定键值所在的桶的索引 +size_type bucket(const key_type& key) const +{ + // 调用底层哈希表的 bucket 方法,获取键值所在桶的索引 + return ht_.bucket(key); +} + +// 返回容器的负载因子,即元素数量与桶数量的比值 +float load_factor() const noexcept { return ht_.load_factor(); } + +// 获取或设置容器的最大负载因子 +float max_load_factor() const noexcept { return ht_.max_load_factor(); } +void max_load_factor(float ml) { ht_.max_load_factor(ml); } + +// 重新哈希容器,指定新的桶数量 +void rehash(size_type count) { ht_.rehash(count); } + +// 预留空间,指定容器需要容纳的元素数量 +void reserve(size_type count) { ht_.reserve(count); } + +// 返回容器使用的哈希函数 +hasher hash_fcn() const { return ht_.hash_fcn(); } + +// 返回容器使用的键值比较函数 +key_equal key_eq() const { return ht_.key_eq(); } +``` +public: + // 重载相等操作符,用于比较两个 unordered_set 容器是否相等 + friend bool operator==(const unordered_set& lhs, const unordered_set& rhs) + { + // 调用底层哈希表的 equal_range_unique 函数,比较两个容器的元素是否相等 + return lhs.ht_.equal_range_unique(rhs.ht_); + } + + // 重载不等操作符,用于比较两个 unordered_set 容器是否不相等 + friend bool operator!=(const unordered_set& lhs, const unordered_set& rhs) + { + // 利用相等操作符的结果来判断两个容器是否不相等 + return !(lhs == rhs); + } +/*****************************************************************************************/ +// 模板类 unordered_multiset,用于存储允许重复的键值。 +// 参数一代表键值类型,参数二代表哈希函数,默认使用 mystl::hash, +// 参数三代表键值比较方式,默认使用 mystl::equal_to。 +template , class KeyEqual = mystl::equal_to> +class unordered_multiset +{ +private: + // 使用 hashtable 作为底层机制来存储数据。 + typedef hashtable base_type; + base_type ht_; // 实际存储数据的哈希表对象 + +public: + // 使用 hashtable 的型别定义 + typedef typename base_type::allocator_type allocator_type; // 分配器类型 + typedef typename base_type::key_type key_type; // 键类型 + typedef typename base_type::value_type value_type; // 值类型 + typedef typename base_type::hasher hasher; // 哈希函数类型 + typedef typename base_type::key_equal key_equal; // 键值比较函数类型 + typedef typename base_type::size_type size_type; // 大小类型 + typedef typename base_type::difference_type difference_type; // 差异类型 + typedef typename base_type::pointer pointer; // 指针类型 + typedef typename base_type::const_pointer const_pointer; // 常量指针类型 + typedef typename base_type::reference reference; // 引用类型 + typedef typename base_type::const_reference const_reference; // 常量引用类型 + typedef typename base_type::const_iterator iterator; // 迭代器类型 + typedef typename base_type::const_iterator const_iterator; // 常量迭代器类型 + typedef typename base_type::const_local_iterator local_iterator; // 本地迭代器类型 + typedef typename base_type::const_local_iterator const_local_iterator; // 常量本地迭代器类型 + + // 获取分配器 + allocator_type get_allocator() const { return ht_.get_allocator(); } + +public: + // 构造、复制、移动函数 + + // 默认构造函数,初始化哈希表为默认大小和哈希函数 + unordered_multiset() + :ht_(100, Hash(), KeyEqual()) + { + } + + // 自定义构造函数,允许指定桶的数量、哈希函数和键值比较函数 + explicit unordered_multiset(size_type bucket_count, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + :ht_(bucket_count, hash, equal) + { + } + + // 范围构造函数,从输入迭代器范围中初始化 + template + unordered_multiset(InputIterator first, InputIterator last, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + : ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) + { + for (; first != last; ++first) + ht_.insert_multi_noresize(*first); // 插入元素,不重新分配空间 + } + + // 列表初始化构造函数 + unordered_multiset(std::initializer_list ilist, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + :ht_(mystl::max(bucket_count, static_cast(ilist.size())), hash, equal) + { + for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) + ht_.insert_multi_noresize(*first); // 插入元素,不重新分配空间 + } + + // 复制构造函数 + unordered_multiset(const unordered_multiset& rhs) + :ht_(rhs.ht_) + { + } + + // 移动构造函数 + unordered_multiset(unordered_multiset&& rhs) noexcept + : ht_(mystl::move(rhs.ht_)) + { + } + + // 复制赋值运算符 + unordered_multiset& operator=(const unordered_multiset& rhs) + { + ht_ = rhs.ht_; + return *this; + } + + // 移动赋值运算符 + unordered_multiset& operator=(unordered_multiset&& rhs) + { + ht_ = mystl::move(rhs.ht_); + return *this; + } + // 列表赋值运算符 + unordered_multiset& operator=(std::initializer_list ilist) + { + ht_.clear(); // + ht_.reserve(ilist.size()); + for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) + ht_.insert_multi_noresize(*first); // 插入元素,不重新分配空间 + return *this; // + } + // + // 析构函数 + ~unordered_multiset() = default; // 默认析构函数,由编译器自动生成,用于释放资源 + + // 迭代器相关函数 + + // 返回指向容器开始的迭代器 + iterator begin() noexcept { return ht_.begin(); } + const_iterator begin() const noexcept { return ht_.begin(); } + + // 返回指向容器结束的迭代器 + iterator end() noexcept { return ht_.end(); } + const_iterator end() const noexcept { return ht_.end(); } + + // 返回指向容器开始的常量迭代器 + const_iterator cbegin() const noexcept { return ht_.cbegin(); } + const_iterator cend() const noexcept { return ht_.cend(); } + + // 容量相关函数 + + // 判断容器是否为空 + bool empty() const noexcept { return ht_.empty(); } + // 返回容器中元素的数量 + size_type size() const noexcept { return ht_.size(); } + // 返回容器可以容纳的最大元素数量 + size_type max_size() const noexcept { return ht_.max_size(); } + }; + + // 修改容器相关 + + // emplace / emplace_hint + // emplace 函数用于在容器中直接构造一个新元素,参数是构造函数需要的参数 + // emplace_hint 函数与 emplace 类似,但提供了一个迭代器 hint 作为插入位置的提示 + template + iterator emplace(Args&& ...args) + { + // 调用底层哈希表的 emplace_multi 函数,尝试插入一个新元素 + // mystl::forward 用于完美转发参数,保留参数的引用类型 + return ht_.emplace_multi(mystl::forward(args)...); + } + + template + iterator emplace_hint(const_iterator hint, Args&& ...args) + { + // 调用底层哈希表的 emplace_multi_use_hint 函数,尝试插入一个新元素 + // hint 作为插入位置的提示,可以提高插入效率 + return ht_.emplace_multi_use_hint(hint, mystl::forward(args)...); + } + + // insert + // insert 函数用于将一个元素插入容器中,允许重复 + iterator insert(const value_type& value) + { + // 调用底层哈希表的 insert_multi 函数,插入一个新元素 + return ht_.insert_multi(value); + } + + iterator insert(value_type&& value) + { + // 调用底层哈希表的 emplace_multi 函数,插入一个新元素 + // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制 + return ht_.emplace_multi(mystl::move(value)); + } + + iterator insert(const_iterator hint, const value_type& value) + { + // 调用底层哈希表的 insert_multi_use_hint 函数,插入一个新元素 + // hint 作为插入位置的提示,可以提高插入效率 + return ht_.insert_multi_use_hint(hint, value); + } + + iterator insert(const_iterator hint, value_type&& value) + { + // 调用底层哈希表的 emplace_multi_use_hint 函数,插入一个新元素 + // hint 作为插入位置的提示,可以提高插入效率 + // mystl::move 用于将右值引用转换为左值引用,避免不必要的复制 + return ht_.emplace_multi_use_hint(hint, mystl::move(value)); + } + + template + void insert(InputIterator first, InputIterator last) + { + // 调用底层哈希表的 insert_multi 函数,将一个范围内的元素插入容器中 + ht_.insert_multi(first, last); + } + + // erase / clear + // erase 函数用于从容器中删除元素 + void erase(iterator it) + { + // 调用底层哈希表的 erase 函数,删除指定位置的元素 + ht_.erase(it); + } + + void erase(iterator first, iterator last) + { + // 调用底层哈希表的 erase 函数,删除指定范围内的元素 + ht_.erase(first, last); + } + + size_type erase(const key_type& key) + { + // 调用底层哈希表的 erase_multi 函数,删除指定键值的所有元素 + return ht_.erase_multi(key); + } + + void clear() + { + // 调用底层哈希表的 clear 函数,清空容器中的所有元素 + ht_.clear(); + } + + void swap(unordered_multiset& other) noexcept + { + // 调用底层哈希表的 swap 函数,交换两个容器的内容 + ht_.swap(other.ht_); + } + + // 查找相关 + // count 函数用于返回指定键值的元素个数 + size_type count(const key_type& key) const + { + // 调用底层哈希表的 count 函数,返回指定键值的元素个数 + return ht_.count(key); + } + + // find 函数用于查找指定键值的元素,并返回指向该元素的迭代器 + iterator find(const key_type& key) + { + // 调用底层哈希表的 find 函数,查找指定键值的元素 + return ht_.find(key); + } + + const_iterator find(const key_type& key) const + { + // 调用底层哈希表的 find 函数,查找指定键值的元素 + return ht_.find(key); + } + + // equal_range 函数用于查找指定键值的元素范围,并返回一个迭代器对 + pair equal_range(const key_type& key) + { + // 调用底层哈希表的 equal_range_multi 函数,查找指定键值的元素范围 + return ht_.equal_range_multi(key); + } + + pair equal_range(const key_type& key) const + { + // 调用底层哈希表的 equal_range_multi 函数,查找指定键值的元素范围 + return ht_.equal_range_multi(key); + } + + // bucket interface + // begin 函数用于返回指向指定桶的开始的迭代器 + local_iterator begin(size_type n) noexcept + { + // 调用底层哈希表的 begin 函数,返回指向指定桶的开始的迭代器 + return ht_.begin(n); + } + + const_local_iterator begin(size_type n) const noexcept + { + // 调用底层哈希表的 begin 函数,返回指向指定桶的开始的迭代器 + return ht_.begin(n); + } + + const_local_iterator cbegin(size_type n) const noexcept + { + // 调用底层哈希表的 cbegin 函数,返回指向指定桶的开始的常量迭代器 + return ht_.cbegin(n); + } + + // end 函数用于返回指向指定桶的结束的迭代器 + local_iterator end(size_type n) noexcept + { + // 调用底层哈希表的 end 函数,返回指向指定桶的结束的迭代器 + return ht_.end(n); + } + + const_local_iterator end(size_type n) const noexcept + { + // 调用底层哈希表的 end 函数,返回指向指定桶的结束的迭代器 + return ht_.end(n); + } + + const_local_iterator cend(size_type n) const noexcept + { + // 调用底层哈希表的 cend 函数,返回指向指定桶的结束的常量迭代器 + return ht_.cend(n); + } + + // bucket_count 函数用于返回容器中的桶数量 + size_type bucket_count() const noexcept + { + // 调用底层哈希表的 bucket_count 函数,返回容器中的桶数量 + return ht_.bucket_count(); + } + // max_bucket_count 函数用于返回容器中允许的最大桶数量。 + size_type max_bucket_count() const noexcept + { + // 调用底层哈希表的 max_bucket_count 函数,返回容器中允许的最大桶数量。 + return ht_.max_bucket_count(); // + } + + // bucket_size 函数用于返回指定桶中的元素数量。 // + size_type bucket_size(size_type n) const noexcept + { // + // 调用底层哈希表的 bucket_size 函数,返回指定桶中的元素数量。 + return ht_.bucket_size(n); + } + // + // bucket 函数用于返回指定键值所在的桶的索引。 + size_type bucket(const ke //