In this article, you will learn about the unorderedmultimap keyeq function in C++ with its syntax and examples.
What is unordered_multimap key_eq function?
In C++ language, the unorderedmultimap is a container that allows multiple elements with the same key. In this function, duplicate keys are allowed. The keyeq 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:
unordered_multimap_name.key_eq() (arg1, arg2);
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++:
#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 us take an example to illustrate the key_eq function in C++.
#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.