Skip to content
Related Articles

Related Articles

unordered_map key_eq() function in C++ STL

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Dec, 2018
View Discussion
Improve Article
Save Article

unordered_map::key_eq() is a built-in function in C++ STL which returns a boolean value according to the comparison. It depends on the key equivalence comparison predicate used by the unordered_map container. The key equivalence comparison is a predicate which takes two arguments and returns a boolean value indicating whether they are to be considered equivalent. It returns true if they are equivalent else it returns false. It is adopted by the container on construction and is similar to the (==) operator used in the comparison.

Syntax

unordered_map_name.key_eq()(args1, args2)

Parameter: The function accepts two mandatory parameters args1 and args2, between whom the comparison is to be done. The data_type is same as that of the unordered_map.

Return Value: The function returns a boolean value.

Below programs illustrate the unordered_map::key_eq() function.

Example 1:




// CPP program to illustrate the
// unordered_map::key_eq() function
  
#include <bits/stdc++.h>
  
using namespace std;
  
int main()
{
    // Declaring unordered_map
    unordered_map<string, string> sample;
  
    // check details
    bool answer
        = sample.key_eq()("GEEKS", "geeks");
  
    // checks if both are same
    if (answer)
        cout << "GEEKS and geeks are treated"
             << " similarly in the container\n";
    else
        cout << "GEEKS and geeks are treated"
             << " dissimilarly in the container\n";
  
    return 0;
}


Output:

GEEKS and geeks are treated dissimilarly in the container

Example 2:




// CPP program to illustrate the
// unordered_map::key_eq() function
  
#include <bits/stdc++.h>
  
using namespace std;
  
int main()
{
  
    unordered_map<int, int> sample;
  
    bool answer = sample.key_eq()(100, 200);
  
    // check
    if (answer)
        cout << "100 and 200 are treated "
             << "similarly in the container\n";
    else
        cout << "100 and 200 are treated"
             << " dissimilarly in the container\n";
  
    answer = sample.key_eq()(100, 100);
    if (answer)
        cout << "100 and 100 are treated "
             << "similarly in the container\n";
    else
        cout << "100 and 100 are treated "
             << "dissimilarly in the container\n";
  
    return 0;
}


Output:

100 and 200 are treated dissimilarly in the container
100 and 100 are treated similarly in the container

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!