unordered_set key_eq() function in C++ STL

The unordered_set key_eq() is a built-in function in C++ STL which returns a boolean value according to the comparison. It returns the key equivalence comparison predicate used by the unordered_set. The key equivalence comparison is a predicate that takes two arguments and returns a bool value indicating whether they are equal.

Syntax:

key_equal key_eq() const

Return Value: This method returns the key equality comparison object.

Time Complexity: O(1)

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
  
int main()
{
  
    // unordered_set ms is created
    unordered_set<string> ms;
  
    bool res = ms.key_eq()("a", "A");
  
    cout << "ms.key_eq() is ";
  
    if (res == 1) {
  
        cout << "case insensitive";
    }
    else {
  
        // res is 0 as arguments are not equivalent
        cout << "case sensitive";
    }
  
    cout << "\n";
  
    return 0;
}

chevron_right


Output:

ms.key_eq() is case sensitive

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
  
int main()
{
  
    // unordered_set mp is created
    unordered_set<string> mp;
  
    // the 2 strings are compared
    bool
        r
        = mp.key_eq()(
            "1000 is a huge number",
            "2000 is a huge number");
  
    cout << "strings are ";
  
    if (r == 1) {
  
        cout << "same";
    }
    else {
  
        // the strings are not same so r=0
        cout << "not same";
    }
  
    cout << "\n";
  
    return 0;
}

chevron_right


Output:

strings are not same


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.