Javatpoint Logo
Javatpoint Logo

C++ Unordered_Mutimap

An associative container is an unordered multimap. It stores key-value pairs similarly to how an unordered map does. On the other hand, repeated values are acceptable in multimaps but not in unordered maps. These are unordered containers, so there is no order in the process of storing stuff. Though, the duplicates are permitted so things with the same key are gathered in the same bucket. For duplicate keys, each key-value pair has a unique count value.

The internal hash algorithm establishes the temporal complexity of unordered multimaps, which store key-value pairs in a hash table. Any operation takes a constant amount of time on average, and a linear amount of time on worst.

Syntax:

The syntax for unordered_multimap in the C++ STL is as follows.

Example:

Let's take an example to illustrate the use of unordered_multimap function in C++.

Output:

Unordered multimap contains:
apple = 3
apple = 4
banana = 2
cherry = 5
date = 1

Methods of Unordered Multimap:

Below is a list of some of the unordered_multimap's often used functions:

begin() -

It returns an iterator pointing to the first element of either the container or one of its buckets.

end() -

It provides an iterator that points to the location following the final element of either the container or one of its buckets.

count() -

It returns the number of container items whose key corresponds to the key of the parameter.

cbegin() -

It returns a constant iterator that points to the first element of either the container or one of its buckets.

cend() -

It provides a constant iterator that points to the position following the final element of either the container or one of its buckets.

clear() -

It clears the contents of the unordered multimap container.

size() -

It returns the size of the unordered multimap. It shows the quantity of goods inside the container.

swap() -

It switches the contents of two unordered multimap containers. The two containers may have different sizes.

find() -

It returns an iterator pointing to an entry with the key k; bucket size() - Returns the number of elements in the bucket n.

find() -

This function returns true if the unordered multimap container is empty. False is returned if not.

The function max size() yields the highest quantity of items that the unordered multimap container is capable of holding.

emplace() -

It adds a new key element to the unordered multimap container.

Example:

In the example below, a few of these techniques are being used.

Output:

Initial unordered multimap:
apple = 5
banana = 3
banana = 4
cherry = 8
cherry = 6
date = 2

Size of the unordered multimap is 6
Key banana is there in the unordered multimap
Value associated with banana is 3
Number of values with banana are 2

After insertion:
apple = 5
banana = 3
banana = 4
cherry = 8
cherry = 6
date = 2
date = 6
fig = 7
grape = 9

After deletion of apple:
banana = 3
banana = 4
cherry = 8
cherry = 6
date = 2
date = 6
fig = 7
grape = 9
unordered multimap is empty

Time Complexity:

Depending on the internal hash algorithm being utilized, all operations on an unordered multimap typically require the same length of time. In the worst case, time can grow to linearity. But multimap (tree-based multimap) is inferior to unordered multimap.

In the usual case, linear, that is, O(n).

In the worst case, O(n^2), or quadratic.







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