Unordered Multimap Key Eq Function In C++ - C++ Programming Tutorial
C++ Course / STL Set & Map / Unordered Multimap Key Eq Function In C++

Unordered Multimap Key Eq Function In C++

BLUF: Mastering Unordered Multimap Key Eq Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Unordered Multimap Key Eq Function In C++

C++ is renowned for its efficiency. Learn how Unordered Multimap Key Eq Function In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, you will explore the keyeq method within the unorderedmultimap container in C++, along with its syntax and practical illustrations.

What is unordered_multimap key_eq function?

In the C++ programming language, the unorderedmultimap serves as a data structure that permits the storage of multiple elements sharing identical keys. Within this context, redundant keys are considered valid. The keyeq member method constitutes an integral component of the unordered_multimap class, offering a means to retrieve the key equality comparison entity linked with the container.

This function will provide a boolean output. It operates similarly to the == operator. When the keys are identical, the function will yield true. In cases where the keys differ, it will result in false. Typically, this function is internally employed by the unordered_multimap for executing key comparison operations.

Syntax:

It has the following syntax:

Example

unordered_multimap_name.key_eq() (arg1, arg2);

This function requires two obligatory parameters, arg1 and arg2.

These two are compared and return the boolean.

Example 1:

Let's consider an example to demonstrate the key_eq function in C++:

Example

#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
 unordered_multimap<int, string> taskMap;
 taskMap.insert({1, "Task A"});
 taskMap.insert({2, "Task B"});
 taskMap.insert({1, "Task C"});
 taskMap.insert({3, "Task D"});
 auto keyEqual = taskMap.key_eq();
 int priority1 = 1;
 int priority2 = 2;
 bool prioritiesEqual = keyEqual(priority1, priority2);
 cout << "Priority " << priority1 << " and Priority " << priority2
 << " are equal: " << boolalpha << prioritiesEqual << endl;
 return 0;
}

Output:

Explanation:

We will discuss the program step by step:

  • First of all, the headers in the file are iostream and unorderedmap . We all know that the iostream is used for input and output handling. At the same time, the unorderedmap is included to use the unordered_map container, which allows multiple keys.
  • There are four variables in the program: taskMap , which is the name of the unorderedmultimap we are using in the program; keyEqual to store the key equality comparison object obtained from taskMap.keyeq, priority1 and priority2 represent two different priority levels. They are used for testing key equality.
  • 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's consider an example to demonstrate the key_eq function in C++.

Example

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class TaskManager {
public:
 void addTask(int priority, const string& task) {
 tasks.insert({priority, task});
 }
 void printTasks() const {
 cout << "Tasks:\n";
 for (const auto& task : tasks) {
 cout << "Priority " << task.first << ": " << task.second << '\n';
 }
 }
 bool arePrioritiesEqual(int priority1, int priority2) const {
 return tasks.key_eq()(priority1, priority2);
 }
private:
 unordered_multimap<int, string> tasks;
};
int main() {
 TaskManager taskManager;
 taskManager.addTask(3, "Go to the gym");
 taskManager.addTask(1, "Finish work project");
 taskManager.addTask(2, "Buy groceries");
 taskManager.addTask(1, "Attend meeting");
 taskManager.printTasks();
 int priority1 = 1;
 int priority2 = 2;
 bool result = taskManager.arePrioritiesEqual(priority1, priority2);
 if (result){
 cout << "Priority " << priority1 << " and Priority " << priority2 << " are equal " << endl; 
 }
 else{
 cout << "Priority " << priority1 << " and Priority " << priority2 << " are not equal " << endl; 
 }
 priority1 = 1;
 priority2 = 1;
 result = taskManager.arePrioritiesEqual(priority1, priority2);
 if (result){
 cout << "Priority " << priority1 << " and Priority " << priority2 << " are equal " << endl; 
 }
 else{
 cout << "Priority " << priority1 << " and Priority " << priority2 << " are not equal " << endl; 
 }
 return 0;
}

Output:

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 keyeq 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 unorderedmultimap. If a custom comparison logic were needed, it could be implemented in a custom keyequal functor and passed to the unorderedmultimap during its declaration.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience