C++ Unordered_set::key_eq() Function
The C++ std::unordered_set::key_eq() function is used to return the boolean value according to the comparison, it returns true if the equivalence occurs; otherwise, it returns false that depends on the key equivalence comparison predicate used by the unordered_set container that compares elements for equality.
The key equivalence comparison is a predicate that takes two arguments of the key type and returns a boolean value indicating whether they are to be considered equivalent.
Syntax
Following is the syntax of std::unordered_set::key_eq() function.
key_equal key_eq() const;
Parameters
This function does not accepts any parameter.
Return Value
This function returns a key equality comparison object.
Example 1
In the following example, we are going to use the unordered_set::key_eq() function and check whether the function is case-sensitive or not.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main () {
unordered_set<string> uSet;
bool case_insensitive = uSet.key_eq()("jerry","JERRY");
cout << "uSet.key_eq() is ";
cout << ( case_insensitive ? "case insensitive" : "case sensitive" );
cout << endl;
return 0;
}
Output
If we run the above code it will generate the following output −
uSet.key_eq() is case sensitive
Example 2
Consider the following example, where we are going to check whether the unordered_set is similar or not using the e are using key_eq() function.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main () {
unordered_set<string> uSet;
bool equal = uSet.key_eq()("tutorialspoint","tutorialspoint");
if(equal){
cout<<"both elements are similar\n";
} else{
cout<<"dissimilar\n";
}
return 0;
}
Output
Following is the output of the above code −
both elements are similar
Example 3
Let's look at the following example, where we are going to use the key_eq() function and check whether the given elements are similar or not.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main() {
unordered_set<int> uSet;
bool r = uSet.key_eq()( 105, 115);
cout << "Integers are ";
if (r == 1) {
cout << "same";
} else {
cout << "not same";
}
return 0;
}
Output
Output of the above code is as follows −
Integers are not same