Javatpoint Logo
Javatpoint Logo

Multimap size() function in C++

In this article, we will discuss the multimap size() function in C++. But before going to the size() function, we must know about the multimap.

Multimap is a sorted container in C++, present in the standard template library. Usually, maps stores the Key and value pairs where keys cannot be duplicated, but multimap stores the Key and value pairs where keys can be repeated. This container is implemented as a Red-Black Tree, a type of balanced binary search tree that ensures efficient searching and insertion operations.

Syntax:

It has the following syntax:

Here, Key is the type of the Key, and T represents the type of the associated value.

Characteristics of the Multimap:

The main characteristics of the multimap are as follows:

Key sorting:

This multimap is ordered based on keys to make the multimap sorted.

Allow duplicate keys:

This feature is useful when one too many relationships between keys and values are required.

Time complexity:

Implementation of multimap as a Balanced Binary Search Tree will provide logarithmic time complexity for key-based operations.

Common operations:

  • The insert() function is used to insert a key and value pair into a multimap
  • The erase() function removes all elements with a specific key from a multimap
  • The size() function is used to return a number of elements in the multimap. This function becomes particularly useful when assessing the overall scale of the data structure.

Example:

Let us take a C++ program to illustrate the multimap implementation.

Output:

Multimap size() function in C++

Explanation:

This fundamental program contains three basic things: declaring the multimap, storing the key and value pairs into the multimap and then retrieving and printing them. Here, the multimap name is studentScores, and a for loop is used to print the Key and value pairs. They are printed in sorted order. In the multimap, Ram has appeared twice because multimap will allow duplicates.

Example:

Let us take another C++ program to illustrate the size() function with the multimap.

Output:

Multimap size() function in C++

Explanation:

  • First, we discuss the headers used in the program.
  • After that, the iostream is used to handle the input and output streams and for displaying the result.
  • The map header is an STL container for associative arrays that are key-value pairs.
  • The string header is used for getting string-related functions and handling text.
  • The variable used in the program is a calendar, which is a multimap and stores the key-value pairs.

Usage of the multimap

The calendar is a multimap that can store duplicate keys and value pairs. Each Key in the multimap represents data in the format "YYYY-MM-DD". After that, the events are added to the calendar. Displaying the total number of events by using the size() function. Iterating through the multimap and prints each data along with the associated events description. The entry.first represents the date and the entry.second represents the event description.

The program output would display the total number of events in the calendar and then print each date along with the respective event descriptions. The order of events might vary because multimap maintains elements in sorted order based on keys.

Conclusion:

The multimap, with its size() function, provides a versatile solution for scenarios requiring the storage of duplicate keys. Its balanced binary search tree implementation ensures efficient operations, and the size() function becomes invaluable for understanding the scale of the data structure. Whether managing student scores, scheduling events, or tackling various other scenarios, the combination of multimap and its size() function is a valuable asset in the C++ programmer's toolkit. As developers explore and utilize these features, they gain a deeper understanding of the container's capabilities and the efficient handling of associative data.







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