HashFn in unordered map video

I did not understand what we did inside the HashFn Class in this unordered Map STL video. I did not understand the roll of size_t data type and our return value. Does this HashFn class overloads our default hash function? and why is the overloading as ’ size_t operator( )(Student &s) ’ .
Can you explain this class step by step?

Hello @PranavM16 size_t is the alias of the unsigned int.
and when we are returning we have concatenated the last name to the first name :
if you still have any doubt you can ask here:
Happy Learning!!

I don’t think we concatenated anything. Length of First name and Last name were added then returned.
Also what operator are we overloading in this function?

Let me make it easy for you, just explain me this code please :slight_smile:

class Student{
public:
    string fname;
    string lname;
    int roll;

    Student(string fname, string lname, int roll){
        this->fname=fname;
        this->lname=lname;
        this->roll=roll;
    }

    bool operator==(const Student &s)const{
        return roll == s.roll;
    }
};

class HashFn{
public:
    size_t operator()(const Student &s)const{
        return s.fname.length() + s.lname.length(); 
    }
};

int main(){
    unordered_map<Student,int,HashFn> umap;

Also another doubt, what does this operator ( ) (parameters) mean? In some tutorials this was used. I thought overloading is done like this operator== (parameters) . @tusharaggarwal272

Hello @PranavM16 this is the fucntion overloading.
and for == the reason is this:
Compiler has zero idea about user defined datatype , so it is always our responsibility to teach compiler how to hash it or how to compare them etc.

And that is why we are overloading == here as well

1 Like

This was in the tutorial operator ( ) (parameters). What operator is this overloading? @tusharaggarwal272

Hello @PranavM16 these are functors:


this will help you.
Happy Learning!!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like