C++ std::unordered_map类型

class template

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 maps are associative containers that store elements formed by the combination of a key value and a mapped value, and which allows for fast retrieval of individual elements based on their keys.

In an unordered_map, the key value is generally used to uniquely identify the element, while the mapped value is an object with the content associated to this key. Types of key and mapped value may differ.

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).

unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

Unordered maps implement the direct access operator (operator[]) which allows for direct access of the mapped value using its key value as argument.

Iterators in the container are at least forward iterators.

Container properties


Associative

  • Elements in associative containers are referenced by their key and not by their absolute position in the container.

Unordered

  • Unordered containers organize their elements using hash tables that allow for fast access to elements by their key.

Map

  • Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value.

Unique keys

  • No two elements in the container can have equivalent keys.

Allocator-aware

  • The container uses an allocator object to dynamically handle its storage needs.

Template parameters


Key

  • Type of the key values. Each element in an unordered_map is uniquely identified by its key value.
    Aliased as member type unordered_map::key_type.

T

  • Type of the mapped value. Each element in an unordered_map is used to store some data as its mapped value.
    Aliased as member type unordered_map::mapped_type. Note that this is not the same as unordered_map::value_type (see below).

Hash

  • A unary function object type that takes an object of type key type as argument and returns a unique value of type size_t based on it. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to hash, which returns a hash value with a probability of collision approaching 1.0/std::numeric_limits<size_t>::max().
    The unordered_map object uses the hash values returned by this function to organize its elements internally, speeding up the process of locating individual elements.
    Aliased as member type unordered_map::hasher.

Pred

  • A binary predicate that takes two arguments of the key type and returns a bool. The expression pred(a,b), where pred is an object of this type and a and b are key values, shall return true if a is to be considered equivalent to b. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to equal_to, which returns the same as applying the equal-to operator (a==b).
    The unordered_map object uses this expression to determine whether two element keys are equivalent. No two elements in an unordered_map container can have keys that yield true using this predicate.
    Aliased as member type unordered_map::key_equal.

Alloc

  • Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
    Aliased as member type unordered_map::allocator_type.

In the reference for the unordered_map member functions, these same names (Key, T, Hash, Pred and Alloc) are assumed for the template parameters.

Iterators to elements of unordered_map containers access to both the key and the mapped value. For this, the class defines what is called its value_type, which is a pair class with its first value corresponding to the const version of the key type (template parameter Key) and its second value corresponding to the mapped value (template parameter T):

typedef pair<const Key, T> value_type;

Iterators of a unordered_map container point to elements of this value_type. Thus, for an iterator called it that points to an element of a map, its key and mapped value can be accessed respectively with:

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>)

Naturally, any other direct access operator, such as -> or [] can be used, for example:

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

Member types

The following aliases are member types of unordered_map. They are widely used as parameter and return types by member functions:
在这里插入图片描述

Member functions

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

Capacity

Namefunction
emptyTest whether container is empty (public member function)
sizeReturn container size (public member function)
max_sizeReturn maximum size (public member function)

Iterators

Namefunction
beginReturn iterator to beginning (public member function)
endReturn iterator to end (public member function)
cbeginReturn const_iterator to beginning (public member function)
cendReturn const_iterator to end (public member function)

Element access

Namefunction
operator[]Access element (public member function )
atAccess element (public member function)

Element lookup

Namefunction
findGet iterator to element (public member function)
countCount elements with a specific key (public member function )
equal_rangeGet range of elements with specific key (public member function)

Modifiers

Namefunction
emplaceConstruct and insert element (public member function )
emplace_hintConstruct and insert element with hint (public member function )
insertInsert elements (public member function )
eraseErase elements (public member function )
clearClear content (public member function )
swapSwap content (public member function)

Buckets

Namefunction
bucket_countReturn number of buckets (public member function)
max_bucket_countReturn maximum number of buckets (public member function)
bucket_sizeReturn bucket size (public member type)
bucketLocate element’s bucket (public member function)

Hash policy

Namefunction
load_factorReturn load factor (public member function)
max_load_factorGet or set maximum load factor (public member function )
rehashSet number of buckets (public member function )
reserveRequest a capacity change (public member function)

Observers

Namefunction
hash_functionGet hash function (public member type)
key_eqGet key equivalence predicate (public member type)
get_allocatorGet allocator (public member function)

原文链接:http://www.cplusplus.com/reference/unordered_map/unordered_map/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值