Javatpoint Logo
Javatpoint Logo

unordered_multimap key_eq function in C++

In this article, you will learn about the unordered_multimap key_eq function in C++ with its syntax and examples.

What is unordered_multimap key_eq function?

In C++ language, the unordered_multimap is a container that allows multiple elements with the same key. In this function, duplicate keys are allowed. The key_eq member function is a part of the unordered_multimap class, and it will provide a way to access the key equality comparison object associated with the container.

This function will return a boolean value. It has the same functionality as the ==. If both the keys are the same, the function will return true. Otherwise, it returns the false. This function is typically used internally by the unordered_multimap to perform operations that involve key comparison.

Syntax:

It has the following syntax:

This function will take two mandatory parameters, arg1 and arg2.

These two are compared and return the boolean.

Example 1:

Let us take an example to illustrate the key_eq function in C++:

Output:

unordered_multimap key_eq function in C++

Explanation:

We will discuss the program step by step:

  1. First of all, the headers in the file are iostream and unordered_map. We all know that the iostream is used for input and output handling. At the same time, the unordered_map is included to use the unordered_map container, which allows multiple keys.
  2. There are four variables in the program: taskMap, which is the name of the unordered_multimap we are using in the program; keyEqual to store the key equality comparison object obtained from taskMap.key_eq(), priority1 and priority2 represent two different priority levels. They are used for testing key equality.
  3. When key_eq() is called, it returns the key equality comparison object (keyEqual). After that, this object compares priorities (priority1 and priority2) for equality. The importance lies in the fact that it allows customization of how keys are compared.

Example 2:

Let us take an example to illustrate the key_eq function in C++.

Output:

unordered_multimap key_eq function in C++

Explanation:

  • This program simulates a task management system using a TaskManager The tasks are stored in an unordered_multimap, allowing multiple tasks with the same priority. The program demonstrates adding tasks, printing tasks, and checking whether two priorities are equal.
  • Variables used are "tasks", which is an unordered_multimap, "priority1", and "priority2", representing different priority levels for testing.
  • Function used in the program are addTask(), which adds a task with specified priority to tasks multimap, printTasks() is used to print all tasks along with their priorities, arePrioritiesEqual() used to compare whether two priority levels are equal in the tasks multimap
  • The arePrioritiesEqual function uses the key_eq() function to obtain the key equality comparison object from the tasks multimap. It then uses this object to compare two priority levels (priority1 and priority2) for equality. The importance lies in the flexibility to customize how keys are compared within the unordered_multimap. If a custom comparison logic were needed, it could be implemented in a custom key_equal functor and passed to the unordered_multimap during its declaration.






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