C 11中新特性之 unordered map

2021-07-24 19:13:44 字數 3971 閱讀 4843

2015-03-09 22:17:42      閱讀:

0      收藏:

0[點我收藏+]

標籤:des

class

com使用

si資料

itla

unordered_map和map類似,都是儲存的key-value的值,可以通過key快速索引到value。

不同的是unordered_map不會根據key的大小進行排序,儲存時是根據key的hash值判斷元素是否相同,即unordered_map內部元素是無序的,而map中的元素是按照二叉搜尋樹儲存,進行中序遍歷會得到有序遍歷。

所 以使用時map的key需要定義operator<。而unordered_map需要定義hash_value函式並且過載 operator==。但是很多系統內建的資料型別都自帶這些,那麼如果是自定義型別,那麼就需要自己過載operator《或者 hash_value()了。

結論:如果需要內部元素自動排序,使用map,不需要排序使用unordered_map

c++0x為什麼不把unordered_map定義為hash_map呢?那是因為在新標準出現之前很多庫廠商已經暫用了hash_map這個名詞。因此為了向前相容不得不定義新的unordered_map。

函式原型

template < class key,                                    // unordered_map::key_type

class hash = hash, // unordered_map::hasher

class pred = equal_to, // unordered_map::key_equal

class alloc = allocator< pair> // unordered_map::allocator_type

>

class unordered_map;

key表示建的型別

t表示鍵對映到的hash值的型別

hash是乙個接受乙個引數且型別要與key相容的函式物件。返回值為t型。注意,引數是const引用, 函式是const

pred是乙個接受兩個引數且其型別與key相容的函式物件。返回值為bool型。注意,引數是const引用, 函式是const

成員函式:

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

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

C 11中新特性 型別推導

c 11標準為c 程式語言的第三個官方標準,正式名叫iso iec 14882 2011 information technology programming languages c 在正式標準發布前,原名c 0x。它將取代c 標準第二版iso iec 14882 2003 programming ...

C 11新特性之 nullptr

我們知道在程式設計的世界裡,0有雙重的角色,可以表示整數零,也可以表示乙個空指標。在c語言中,通過預編譯巨集null,可以區分0表示的是零還是 void 0.但是,在c 的世界中,這樣是不可以的。c 中允許函式過載。例如 void foo char void foo int 如果把null定義為0,...

c 11新特性之auto

在早期版本中,auto關鍵字用來宣告具有自動儲存器的區域性變數,auto關鍵字很少被使用,除了靜態變數之外,其它的變數預設是auto的。因此,在c 11中,刪除了原有的功能,並對其重新設計,增加了auto的型別推導功能。template double add t1 a,t2 b int main i...