STL:unordered_set/unordered_multiset(c++11)
unordered_set:容器内的元素无序排列,基于值进行获取单个元素速度非常快。内部根据它们的 hash value 被组织成 buckets(slot)。
unordered_multiset: 操作和 unordered_set 相同,只是 key 可以重复。
Iterators
begin: 有两个类型:container iterator(1); bucket iterator。
end: 同上面的 begin
cbegin:
cend:
Element lookup
find:
count:
equal_range:返回 pair<iterator,iterator>, 其中的迭代器都是 const_iterator,不能修改容器内的值。
Modifiers
emplace:
emplace_hint:
insert:
erase:
clear:
swap:
Buckets
bucket_count: 返回哈希表中槽的个数(a bucket is a slot in the container's internal hash table)
max_bucket_count: 最大槽的个数
bucket_size: 返回在槽 n 中元素的个数(传入参数 n)
bucket: 给定 val ,定位该值所在的槽(槽被编号为 0 to (bucket_count-1))
Hash policy
load_factor: 负载因子,公式为:size / bucket_size。这个因素影响冲突率,当负载因子超过一个指定阈值(通常是:max_load_factor),需要进行槽的动态扩充。这将导致 rehash。
max_load_factor: 默认的 最大负载因子是 1.0(float 类型),也可以通过这个函数进行修改阈值。
rehash: 设置槽的数量。如果比当前的槽多,则强制进行哈希表的重建,并且自动对已有的数据进行 rehash。如果比当前的槽少,则什么都不会发生。注意:rehash(size_type& n) 也许并不把槽号刚好设置为 n 个槽,而是最少是 n 个槽,这样可以防止多次 rehash 造成的性能下降。
reserve: 同 rehash 一样,可以设置当前的槽数,事先设置好 hash table 的最佳大小,避免多次进行 rehash.
Observers
hash_function: 获取 hash 函数(hasher 类型),用法如例:

1 // unordered_set::hash_function 2 #include <iostream> 3 #include <string> 4 #include <unordered_set> 5 6 typedef std::unordered_set<std::string> stringset; 7 8 int main () 9 { 10 stringset myset; 11 12 stringset::hasher fn = myset.hash_function(); 13 14 std::cout << "that: " << fn ("that") << std::endl; 15 std::cout << "than: " << fn ("than") << std::endl; 16 17 return 0; 18 }
key_eq:返回一个在 unordered_set 的容器中判断是否相等的一个函数,返回 bool 值。用法如例:

1 // unordered_set::key_eq 2 #include <iostream> 3 #include <string> 4 #include <unordered_set> 5 6 int main () 7 { 8 std::unordered_set<std::string> myset; 9 10 bool case_insensitive = myset.key_eq()("checking","CHECKING"); 11 12 std::cout << "myset.key_eq() is "; 13 std::cout << ( case_insensitive ? "case insensitive" : "case sensitive" ); 14 std::cout << std::endl; 15 16 return 0; 17 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗