unordered_map key_eq() function in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

GEEKS and geeks are treated dissimilarly in the container

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.