Movatterモバイル変換


[0]ホーム

URL:


cplusplus.com

Reference

unordered_map

class template
<unordered_map>

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 akey value and amapped value, and which allows for fast retrieval of individual elements based on their keys.

In anunordered_map, thekey value is generally used to uniquely identify the element, while themapped value is an object with the content associated to thiskey. Types ofkey andmapped value may differ.

Internally, the elements in theunordered_map are not sorted in any particular order with respect to either theirkey ormapped values, but organized intobuckets depending on their hash values to allow for fast access to individual elements directly by theirkey values (with a constant average time complexity on average).

unordered_map containers are faster thanmap containers to access individual elements by theirkey, 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 themapped value using itskey value as argument.

Iterators in the container are at leastforward iterators.

Container properties

Associative
Elements in associative containers are referenced by theirkey 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 theirkey.
Map
Each element associates akey to amapped 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 equivalentkeys.
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 anunordered_map is uniquely identified by its key value.
Aliased as member typeunordered_map::key_type.
T
Type of the mapped value. Each element in anunordered_map is used to store some data as its mapped value.
Aliased as member typeunordered_map::mapped_type. Note that this is not the same asunordered_map::value_type (see below).
Hash
A unary function object type that takes an object of typekey type as argument and returns a unique value of typesize_t based on it. This can either be a class implementing afunction call operator or a pointer to a function (seeconstructor for an example). This defaults tohash<Key>, which returns a hash value with a probability of collision approaching1.0/std::numeric_limits<size_t>::max().
Theunordered_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 typeunordered_map::hasher.
Pred
A binary predicate that takes two arguments of thekey type and returns abool. The expressionpred(a,b), wherepred is an object of this type anda andb are key values, shall returntrue ifa is to be considered equivalent tob. This can either be a class implementing afunction call operator or a pointer to a function (seeconstructor for an example). This defaults toequal_to<Key>, which returns the same as applying theequal-to operator (a==b).
Theunordered_map object uses this expression to determine whether two element keys are equivalent. No two elements in anunordered_map container can have keys that yieldtrue using this predicate.
Aliased as member typeunordered_map::key_equal.
Alloc
Type of the allocator object used to define the storage allocation model. By default, theallocator class template is used, which defines the simplest memory allocation model and is value-independent.
Aliased as member typeunordered_map::allocator_type.

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

Iterators to elements ofunordered_map containers access to both thekey and themapped value. For this, the class defines what is called itsvalue_type, which is apair class with itsfirst value corresponding to theconst version of thekey type (template parameterKey) and itssecond value corresponding to themapped value (template parameterT):
1
typedef pair<const Key, T> value_type;

Iterators of aunordered_map container point to elements of thisvalue_type. Thus, for an iterator calledit that points to an element of amap, its key and mapped value can be accessed respectively with:
1
2
3
4
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:
1
2
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 ofunordered_map. They are widely used as parameter and return types by member functions:

member typedefinitionnotes
key_typethe first template parameter (Key)
mapped_typethe second template parameter (T)
value_typepair<const key_type,mapped_type>
hasherthe third template parameter (Hash)defaults to:hash<key_type>
key_equalthe fourth template parameter (Pred)defaults to:equal_to<key_type>
allocator_typethe fifth template parameter (Alloc)defaults to:allocator<value_type>
referenceAlloc::reference
const_referenceAlloc::const_reference
pointerAlloc::pointerfor the defaultallocator:value_type*
const_pointerAlloc::const_pointerfor the defaultallocator:const value_type*
iteratoraforward iterator tovalue_type
const_iteratoraforward iterator toconst value_type
local_iteratoraforward iterator tovalue_type
const_local_iteratoraforward iterator toconst value_type
size_typean unsigned integral typeusually the same assize_t
difference_typea signed integral typeusually the same asptrdiff_t
member typedefinitionnotes
key_typethe first template parameter (Key)
mapped_typethe second template parameter (T)
value_typepair<const key_type,mapped_type>
hasherthe third template parameter (Hash)defaults to:hash<key_type>
key_equalthe fourth template parameter (Pred)defaults to:equal_to<key_type>
allocator_typethe fifth template parameter (Alloc)defaults to:allocator<value_type>
referencevalue_type&
const_referenceconst value_type&
pointerallocator_traits<Alloc>::pointerfor the defaultallocator:value_type*
const_pointerallocator_traits<Alloc>::const_pointerfor the defaultallocator:const value_type*
iteratoraforward iterator tovalue_type
const_iteratoraforward iterator toconst value_type
local_iteratoraforward iterator tovalue_type
const_local_iteratoraforward iterator toconst value_type
size_typean unsigned integral typeusually the same assize_t
difference_typea signed integral typeusually the same asptrdiff_t

Member functions

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

Capacity

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

Iterators

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)

Element access

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

Element lookup

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)

Modifiers

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)

Buckets

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)

Hash policy

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)

Non-member function overloads

operators (unordered_map)
Relational operators for unordered_map(function template)
swap (unordered_map)
Exchanges contents of two unordered_map containers(function template)
Home page |Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved -v3.3.4s
Spotted an error? contact us

[8]ページ先頭

©2009-2026 Movatter.jp