C++ Library –
C++ Library – <unordered_map> ”; Previous Next Introduction to unordered_map Unordered map is dictionary like data structure. It is a sequence of (key, value) pair, where only single value is associated with each unique key. It is often referred as associative array. It enables fast retrieval of individual elements based on their keys. It also implements the direct access operator(subscript operator[]) which allows for direct access of the mapped value using its key value as argument. Unordered map does not sort its element in any particular order with respect to either their key or mapped values, instead organizes into buckets depending on their hash values to allow for fast access to individual elements directly by their key values. Unordered map performs better than map while accessing individual elements by their keys. But for range iteration their performance is considerably low. Definition Below is definition of std::unordered_map from <unordered_map> header file template < class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, class Alloc = allocator< pair<const Key,T> > > class unordered_map; Parameters Key − Type of the key. T − Type of the mapped values. Hash − A unary function object type which takes an object of type key type as argument and returns a unique value of type size_t based on it. Pred − A binary predicate that which two arguments of the key type and returns a bool. Alloc − Type of the allocator object. T may be substituted by any other data type including user-defined type. Member types Following member types can be used as parameters or return type by member functions. Sr.No. Member types Definition 1 key_type Key (First parameter of the template) 2 mapped_type T (Second parameter of the template) 3 value_type pair<const key_type,mapped_type> 4 hasher The third template parameter (defaults to: hash<key_type>) 5 key_equal The fourth template parameter (defaults to: equal_to<key_type>) 6 allocator_type Alloc (Fifth parameter of the template) 7 reference value_type& 8 const_reference const value_type& 9 pointer allocator_traits<Alloc>::pointer 10 const_pointer allocator_traits<Alloc>::const_pointer 11 iterator A forward iterator to value_type value_type 12 const_iterator A forward iterator to const value_type value_type 13 local_iterator A forward iterator to value_type 14 const_local_iterator A forward iterator to const value_type 15 difference_type ptrdiff_t 16 size_type size_t Functions from <unordered_map> Below is list of all methods from <unordered_map> header. Constructors Sr.No. Method & Description 1 unordered_map::unordered_map default constructor Constructs an empty unordered_map with zero elements. 2 unordered_map::unordered_map copy constructor Constructs an unordered_map with copy of each elements present in existing unordered_map. 3 unordered_map::unordered_map move constructor Constructs an unordered_map with the contents of other using move semantics. 4 unordered_map::unordered_map range constructor Constructs an unordered_map with as many elements as in range of first to last. 5 unordered_map::unordered_map initializer_list constructor Constructs an unordered_map from initialize list. Destructor Sr.No. Method & Description 1 unordered_map::~unordered_map Destroys unordered_map object by deallocating it”s memory. Member functions Sr.No. Method & Description 1 unordered_map::at Returns a reference to the mapped value associated with key k. 2 unordered_map::begin container iterator Returns an iterator which refers to the first element of the map. 3 unordered_map::begin bucket iterator Returns an iterator pointing to the first element in one of its buckets. 4 unordered_map::bucket Returns the bucket number where element with key k is located. 5 unordered_map::bucket_count Returns the number of buckets in unordered_map container. 6 unordered_map::bucket_size Returns the number of elements presents in the nth bucket. 7 unordered_map::cbegin container iterator Returns a constant iterator which refers to the first element of the unordered_map. 8 unordered_map::cbegin bucket iterator Returns a constant iterator pointing to the first element in one of its buckets. 9 unordered_map::cend container iterator Returns a constant iterator which points to past-the-end element of the unordered_map. 10 unordered_map::cend bucket iterator Returns a constant iterator which points to past-the-end element in one of its buckets. 11 unordered_map::clear Destroys the unordered_map by removing all elements and sets the size of unordered_map to zero. 12 unordered_map::count Returns the number of mapped values associated with key k. 13 unordered_map::emplace Extends container by inserting new element. 14 unordered_map::emplace_hint Inserts a new element in unordered_map using hint as a position for element. 15 unordered_map::empty Tests whether unordered_map is empty or not. 16 unordered_map::end container iterator Returns an iterator which points to past-the-end element in the unordered_map. 17 unordered_map::end bucket iterator Returns an iterator which points to past-the-end element in one of its buckets. 18 unordered_map::equal Returns range of elements that matches specific key. 19 unordered_map::erase position version Removes single element of the unordered_map from position. 20 unordered_map::erase key version Removes mapped value associated with key k. 21 unordered_map::erase range version Removes range of element from the the unordered_map. 22 unordered_map::find Finds an element associated with key k. 23 unordered_map::get_allocator Returns an allocator associated with unordered_map. 24 unordered_map::hash_function Calculates the hash function object used by the unordered_map container. 25 unordered_map::insert Extends container by inserting new element in unordered_map. 26 unordered_map::insert move version Extends container by inserting new element in unordered_map. 27 unordered_map::insert hint version Extends conta iner by inserting new element in unordered_map. 28 unordered_map::insert move and hint version Extends unordered_map by inserting new element. 29 unordered_map::insert range version Extends container by inserting new elements in the unordered_map. 30 unordered_map::insert initializer_list version Extends map by inserting new element from initializer list. 31 unordered_map::key_eq Returns the function that compares keys for equality. 32 unordered_map::load_factor Returns the current load factor of the unordered_map container. 33 unordered_map::max_bucket_count Returns the maximum number of buckets that the unordered_map container can have. 34 unordered_map::max_load_factor get version Returns the current maximum load factor for the unordered_map container. 35 unordered_map::max_load_factor set version Assigns new load factor for the unordered_map container. 36 unordered_map::max_size Returns the maximum number of elements can be held by unordered_map. 37 unordered_map::operator= copy version Assigns new contents to the unordered_map by replacing old ones and modifies size if necessary. 38 unordered_map::operator= move version Move the contents of one unordered_map into another and modifies size if necessary. 39 unordered_map::operator= initializer_list version Copy elements from initializer list to unordered_map. 40 unordered_map::operator[] If key k matches an element in