In order to do something like my_set.find("Bar")
you need to do two things:
Your C++ compiler must support the C++20 standard and you must enable C++20 when compiling your code.
Implement your comparison and hash classes to be “transparent”. This boils down to implementing overloads that hash a
std::string
(or aconst 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.