find by hash value from an unordered_set which element is cumtomed type

In order to do something like my_set.find("Bar") you need to do two things:

  1. Your C++ compiler must support the C++20 standard and you must enable C++20 when compiling your code.

  2. Implement your comparison and hash classes to be “transparent”. This boils down to implementing overloads that hash a std::string (or a const char *) in addition to your class, and implementing a comparison operator between your class and a string.

For more information see the reference for std::unordered_set::find.

Before C++20, your only option is to construct a temporary instance of your class and pass it to find instead of a string (this can be done by implementing an appropriate constructor and relying on implicit conversions).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top