Valentin Stamate
Valentin Stamate

Reputation: 1439

Java hashCode() equivalent in c++

Java have a method inside every class called hashCode() witch returns a number. Is there any method to obtain this is c++? I tried std::hash<> from stl but it doesn't work for classes, only primitive types. ex:

Person p;
hash<Person> hs;
cout<<hs(p); // if i replace Person with `int, vector<bool> etc` it works 

Upvotes: 1

Views: 2948

Answers (1)

WolverinDEV
WolverinDEV

Reputation: 1524

In C++ you've no default hash code method.
The reason why std::hash works is that std::hash has a defined implementation for that type.
To define your hash implementation you could lookup this site: https://de.cppreference.com/w/cpp/utility/hash

General notice:
I highly recommend you to get away with your java thoughts since Java and C++ are two totally different languages with different concepts. My recommendation would be to start learning from scratch except that you've already the knowledge about how to solve problems and how to write code. (That's at least what I did)

Upvotes: 2

Related Questions