C++ std::unordered_map

模板: 

template < class Key,                                    // unordered_map::key_type
           class T,                                      // unordered_map::mapped_type
           class Hash = hash<Key>,                       // unordered_map::hasher
           class Pred = equal_to<Key>,                   // unordered_map::key_equal
           class Alloc = allocator< pair<const Key,T> >  // unordered_map::allocator_type
           > class unordered_map;

定义:

unordered_map是和容器相结合去存储那些具有键值和元素值的元素,并且允许通过键值快速检索每个元素。

在一个unordered_map对象中,键值和元素值组成unordered_map的每一个元素,unordered_map中元素的键值必须是相异的。键值和元素值的类型没有必然关系,也就是说键值和元素值的类型可以不相同。

在unordered_map中,所有的元素是无序的。但是在存储的时候通过桶排序,以便于可以通过键值快速地获取每一个元素。

unordered_map容器读取速度比map快,尽管它们通常对元素子集的范围迭代效率较低。

unordered_map实现了 [] 操作符,允许通过键值获取对象元素值。

容器的属性:

关联:关联容器中的元素由它们的键引用,而不是由它们在容器中的绝对位置引用。

无序:无序容器使用哈希表来组织它们的元素,散列表允许通过它们的键快速访问元素。

地图:每个元素都将一个键关联到一个映射值:键值标识其主要内容为映射值的元素。

唯一的键值:容器中的任何两个元素都不能具有相同的键。

Allocator-aware:容器使用一个分配器对象来动态地处理它的存储需求。

模板参数:

Key

键值的类型。unordered_map中的每个元素都由其键值唯一标识。

别名为成员类型unordered_map::key_type。

T

映射值的类型。unordered_map中的每个元素都用于将一些数据存储为其映射值。

别名为成员类型unordered_map::mapped_type。注意,这与unordered_map::value_type(参见下面)不同。

Hash

一个一元函数对象类型,它接受一个key类型的对象作为参数,并基于它返回一个类型size_t的唯一值。它可以是实现函数调用操作符的类,也可以是指向函数的指针(参见构造函数的示例)。默认值是散列<Key>,它返回一个散列值,碰撞概率接近1.0/std::numeric_limits<size_t>::max()。

unordered_map对象使用此函数返回的散列值在内部组织其元素,从而加快了定位单个元素的过程。

别名为成员类型unordered_map::hasher。

Pred

一个二进制谓词,它接受两个键类型的参数并返回一个bool。表达式pred (a, b), pred是这种类型的一个对象,a和b是键值,返回true,如果是应考虑相当于b。这可以是一个类实现一个函数调用操作符或指向函数的指针(见构造函数为例)。这默认为equal_to<Key>,它返回的结果与应用equal-to操作符相同(a==b)。

unordered_map对象使用这个表达式来确定两个元素键是否相等。使用此谓词,unordered_map容器中的任何两个元素都不能具有产生true的键。

别名为成员类型unordered_map::key_equal。

Alloc

用于定义存储分配模型的分配器对象的类型。默认情况下,使用的是分配器类模板,它定义了最简单的内存分配模型,并且是与值无关的。

别名为成员类型unordered_map::allocator_type。

在unordered_map成员函数的引用中,模板参数使用相同的名称(Key、T、Hash、Pred和Alloc)。

unordered_map容器元素的迭代器同时访问键和映射值。为此,该类定义了一个名为value_type的类,它是一个pair类,它的第一个值对应于键类型的const版本(模板参数键),第二个值对应于映射的值(模板参数T):

typedef <const Key,T> value_type;

unordered_map容器的迭代器指向value_type类型的元素。因此,对于一个指向map元素的迭代器来说,它的key和被映射的值可以分别通过以下方式来访问:

unordered_map<Key,T>::iterator it;
(*it).first;             // the key value (of type Key)
(*it).second;            // the mapped value (of type T)
(*it);                   // the "element value" (of type pair<const Key,T>) 

当然,任何其他直接访问操作符,如->或[]都可以使用,例如:

it->first;               // same as (*it).first   (the key value)
it->second;              // same as (*it).second  (the mapped value) 

 

成员类型:

以下别名是unordered_map的成员类型。它们被成员函数广泛用作参数和返回类型:

C++11:

member type definition notes
key_type the first template parameter (Key)  
mapped_type the second template parameter (T)  
value_type pair<const key_type,mapped_type>  
hasher the third template parameter (Hash) defaults to: hash<key_type>
key_equal the fourth template parameter (Pred) defaults to: equal_to<key_type>
allocator_type the fifth template parameter (Alloc) defaults to: allocator<value_type>
reference Alloc::reference  
const_reference Alloc::const_reference  
pointer Alloc::pointer for the default allocator: value_type*
const_pointer Alloc::const_pointer for the default allocator: const value_type*
iterator forward iterator to value_type  
const_iterator forward iterator to const value_type  
local_iterator forward iterator to value_type  
const_local_iterator forward iterator to const value_type  
size_type an unsigned integral type usually the same as size_t
difference_type a signed integral type usually the same as ptrdiff_t

C++14:

member type definition notes
key_type the first template parameter (Key)  
mapped_type the second template parameter (T)  
value_type pair<const key_type,mapped_type>  
hasher the third template parameter (Hash) defaults to: hash<key_type>
key_equal the fourth template parameter (Pred) defaults to: equal_to<key_type>
allocator_type the fifth template parameter (Alloc) defaults to: allocator<value_type>
reference value_type&  
const_reference const value_type&  
pointer allocator_traits<Alloc>::pointer for the default allocator: value_type*
const_pointer allocator_traits<Alloc>::const_pointer for the default allocator: const value_type*
iterator forward iterator to value_type  
const_iterator forward iterator to const value_type  
local_iterator forward iterator to value_type  
const_local_iterator forward iterator to const value_type  
size_type an unsigned integral type usually the same as size_t
difference_type a signed integral type usually the same as ptrdiff_t

函数: 

(constructor) Construct unordered_map (public member function )
(destructor) Destroy unordered map (public member function)
operator= Assign content (public member function )

容量:

empty Test whether container is empty (public member function)
size Return container size (public member function)
max_size Return maximum size (public member function)

迭代器:

begin Return iterator to beginning (public member function)
end Return iterator to end (public member function)
cbegin Return const_iterator to beginning (public member function)
cend Return const_iterator to end (public member function)

获取元素:

operator[] Access element (public member function )
at Access element (public member function)

查找元素:

find Get iterator to element (public member function)
count Count elements with a specific key (public member function )
equal_range Get range of elements with specific key (public member function)
emplace Construct and insert element (public member function )
emplace_hint Construct and insert element with hint (public member function )
insert Insert elements (public member function )
erase Erase elements (public member function )
clear Clear content (public member function )
swap Swap content (public member function)

桶:

bucket_count Return number of buckets (public member function)
max_bucket_count

Return maximum number of buckets (public member function)

bucket_size Return bucket size (public member type)
bucket Locate element's bucket (public member function)

哈希原则:

load_factor Return load factor (public member function)
max_load_factor Get or set maximum load factor (public member function )
rehash Set number of buckets (public member function )
reserve Request a capacity change (public member function)

Observers:

hash_function Get hash function (public member type)
key_eq Get key equivalence predicate (public member type)
get_allocator Get allocator (public member function)

内容来自于:http://www.cplusplus.com/reference/unordered_map/unordered_map/

发布了17 篇原创文章 · 获赞 9 · 访问量 8655

猜你喜欢

转载自blog.csdn.net/qq_39025293/article/details/104483735