How to create an unordered_set of user defined class or struct in C++?

The unordered_set internally implements a hash table to store elements. By default we can store only pre definded type as int, string, float etc.
If we want to store the element of user defined type as structure then compiler will show an error because before storing elements into unordered_set compiler performs some checking. And while comparing two user defined type compiler can not compare them hence it generate an error.
So, in order to store a structure in a unordered_set, some comparison function need to be designed. Since unordered_set also store implements hash table to store elements we should also have to implement hash function to perform hashing related work.
Below method explains its implementation.
Implementation: We create a structure type and define a comparison function inside that structure that will used to compare two structure type objects. Since unordered_set internally implements hash function so we should have also implement the hash function for user defined type objects.
Syntax To store user defined type elements unordered_set should follow following syntax

unordered_set(elementType, MyHashType) us;
// element type is user defined type and MyHashType is class implementing hash function

Below code explains it.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation to use
// user-defined data type in
// structures
#include <bits/stdc++.h>
using namespace std;
  
// Structure definition
struct Test {
  
    int id;
  
    // This function is used by unordered_set to compare
    // elements of Test.
    bool operator==(const Test& t) const
    {
        return (this->id == t.id);
    }
};
  
// class for hash function
class MyHashFunction {
public:
    // id is returned as hash function
    size_t operator()(const Test& t) const
    {
        return t.id;
    }
};
  
// Driver method
int main()
{
    // put values in each
    // structure define below.
    Test t1 = { 110 }, t2 = { 102 },
         t3 = { 101 }, t4 = { 115 };
  
    // define a unordered_set having
    // structure as its elements.
    unordered_set<Test, MyHashFunction> us;
  
    // insert structure in unordered_set
    us.insert(t1);
    us.insert(t2);
    us.insert(t3);
    us.insert(t4);
  
    // printing the elements of unordered_set
    for (auto e : us) {
        cout << e.id << " ";
    }
  
    return 0;
}

chevron_right


Output:

115 101 110 102
Output:

115 101 110 102

Below is another example where we use predefined hash functions to make overall hash function of our defined class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate working of unordered_set
// for user defined data types.
#include <bits/stdc++.h>
using namespace std;
  
struct Person {
    string first, last;
  
    Person(string f, string l)
    {
        first = f;
        last = l;
    }
  
    bool operator==(const Person& p) const
    {
        return first == p.first && last == p.last;
    }
};
  
class MyHashFunction {
public:
  
    // We use predfined hash functions of strings
    // and define our hash function as XOR of the
    // hash values.
    size_t operator()(const Person& p) const
    {
        return (hash<string>()(p.first)) ^ (hash<string>()(p.last));
    }
};
  
// Driver code
int main()
{
    unordered_set<Person, MyHashFunction> us;
    Person p1("kartik", "kapoor");
    Person p2("Ram", "Singh");
    Person p3("Laxman", "Prasad");
  
    us.insert(p1);
    us.insert(p2);
    us.insert(p3);
  
    for (auto e : us) {
        cout << "[" << e.first << ", "
             << e.last << "]\n";
    }
  
    return 0;
}

chevron_right


Output:

[Laxman, Prasad]
[kartik, kapoor]
[Ram, Singh]


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.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.