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/

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章