C++ Unordered_map::key_eq() Function
The C++ functionstd::unordered_map::key_eq() is used to return the boolean value according to the comparison. It returns true if the equivalence occurs; otherwise, it returns false.
The key equivalence comparison is a predicate that takes two arguments of the key type and returns a bool value indicating whether they are to be considered equivalent. Default predicate is equal_to<key_type>, which returns the same as applying the equal-to operator (==) to the arguments.
Syntax
Following is the syntax of std::unordered_map::key_eq() function.
key_equal key_eq() const;
Parameters
This function does not accept any parameter.
Return value
This function returns a boolean value true if the equivalence occurs; otherwise, it returns false.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of the key_eq() function.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
unordered_map<string,string> um;
bool case_insensitive = um.key_eq()("jerry","JERRY");
cout << "mymap.key_eq() is ";
cout << ( case_insensitive ? "case insensitive" : "case sensitive" );
cout << endl;
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
mymap.key_eq() is case sensitive
Example 2
In the following example, we are using key_eq() function to check both key of unordered_map is similar or not.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
unordered_map<string,string> um;
bool equal = um.key_eq()("tutorialspoint","TUTORIALSPOAINT");
if(equal){
cout<<"both are similar\n";
}
else{
cout<<"dissimilar\n";
}
return 0;
}
Output
Following is the output of the above code −
dissimilar
Example 3
Consider the following example, where we are creating an unordered_map that takes integer value and applying the key_eq() function to check both key of unordered_map is similar or not.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
unordered_map<int,int> um;
bool equal = um.key_eq()(105, 105);
if(equal)
cout<<"similar\n";
else
cout<<"dissimilar";
return 0;
}
Output
Output of the above code is as follows −
similar