STL:unordered_set(c++11)

unordered_set:容器内的元素无序排列,基于值进行获取单个元素速度非常快。内部根据它们的 hash value 被组织成 buckets(slot)。

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

猜你喜欢

转载自www.cnblogs.com/zpcoding/p/10336184.html