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 }
View Code
复制代码

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 }
View Code
复制代码

 

posted on   爱笑的张飞  阅读(874)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示