Javatpoint Logo
Javatpoint Logo

UNORDERED_MAP IN C++

The unordered map is an associated container that holds elements created by fusing a mapped value with a key value. The element is identified specifically by its key value, and the mapped value is the content related to the key. Keys and values may both be of any established or user-defined type. An unordered map can be thought of as a data structure of the dictionary type that stores elements within itself. The sequential pairs it holds (key, value) enable quick retrieval of a specific element using its individual key.

The key supplied to the map is hashed into the indices of a hash table, which is why the speed of the data structure heavily depends on the hash function, but on average, the cost of search, insert, and delete from the hash table is o(1).

In the worst case, especially for large prime integers, its time complexity can range from o(1) to o(n). It is highly recommended to utilise a map in this case to avoid receiving a tle (time limit exceeded) issue.

Syntax:

Example:

Output

Distribute 40
Regular 30
Javatpoint 20

Explanation:

This output specifically justifies the fact that the unordered map's output value is generated in a random key-to-value manner while the map shows value and key in an ordered fashion.

Unordered set vs Unordered map

Some differences between Unordered set and Unordered map are as follows:

Unordered map

  • Only (key-value) pairs are found in the elements of an unordered map.
  • Use the operator "[]" to extract a key's corresponding value from a map.

Unordered set

  • Key-value pairs are mostly utilised to determine whether a set is present or absent and are not always present in an unordered set.
  • Using the find() function, an element is searched for. Thus, there is no need for an operator.

Important point:

For instance, take the issue of counting the frequency of individual words. Since, counts cannot be stored in unordered set (or set), we must instead use unordered map.

Map vs. Unordered map

Some differences between the Map and Unordered map are as follows:

Unordered map

  • Any order may be used to store the unordered map key.
  • The implementation of unordered map results in an uneven tree structure, making it impossible to retain the order of the entries.
  • Operations on an unordered map typically have an o(1) time complexity.

Map

  • The map is an ordered list of distinct keys.
  • It is possible to preserve the elements' order (by specific tree traversal) because map uses a balanced tree structure.
  • The map operations have an o time complexity (log n).

Procedures for unordered map

There are numerous functions that can be used with unordered map. The ones who are most helpful are:

  • Operator =
  • Operator[]
  • Beginning and ending of the iterator
  • Empty
  • Size of the capacity
  • For a lookup, locate and count.
  • Insert and delete

The full list of an unordered map's methods is shown below:

At():

This c++ unordered map method returns a reference to the value with the specified element as the key k.

Begin():

It provides a return value that is an iterator pointing to the first entry in the unordered map container.

End():

The unordered map container bucket returns an iterator pointing to the location after the final element ().

Bucket():

It returns the bucket number in the map's bucket count where the element with key k is placed.

Bucket_count()

The unordered map's total number of buckets is tallied using the bucket count function. It can be called without passing any parameters.

Bucket size

It gives the unordered map count's element count for each bucket ().

Count()

It gives the unordered map count's element count for each bucket () the number of elements in an unordered map with the specified key equal range should be counted.

Equal_eange()

It returns the boundaries of a range with all the container's items and a key that compares to k.

Find()

Gives an iterator to the element's empty.

Position ()

It determines whether the unordered map container's container is empty.

Erase()

Elements in the unordered map container can be deleted using the erase() function.

Although the functions to view the internal bucket size, bucket count, used hash function, and various hash policies are also provided by the c++11 library, they are less helpful in practical applications. Using iterator, we may loop through every element in the unordered map.

Example:

Output

Retrieved the value of pi

Lambda value cannot retrieved

The entire elements :
E 2.718
The value ofloge 1
The value oflog10 2.302
The value of root2 1.414
The value ofroot3 1.732
The value of pi 3.14
Two 2
Three 3
One 1

Example:

Output

(programs, 1)
(learn, 1)
(questions, 1)
(t, 1)
(points, 1)
(java, 1)






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA