c++ - unordered_map and pointers with value semantics -


i've defined type term accessed pointer has value semantics; 2 different term objects logically equal if have same contents. need use them map keys.

a first attempt might unordered_map<term*, int>, doesn't value semantics. needs custom hash , equality comparison functions. (for number of reasons, want define these globally instead of having supply them template arguments in every case.)

the custom hash function can iirc defined like

namespace std { template <> struct hash<term*> ... 

but unordered_map compares keys ==, can't define custom operators on built-in types, , pointers built-in types.

i define wrapper class holds term* , use key. there simpler solution?

if @ definition of unordered_map:

template<     class key,     class t,     class hash = std::hash<key>,     class keyequal = std::equal_to<key>,     class allocator = std::allocator< std::pair<const key, t> > > class unordered_map; 

you can see can define custom operator comparison fourth template argument. in case supply them once @ unordered_map creation without "having supply them template arguments in every case":

struct custom_operator {     bool operator()(term* lhs, term* rhs) const { ... } }; 

and use as:

std::unordered_map< term*, int                   , std::hash<term*>                   , custom_operator> map; 

in case highly recommend not using pointer key hash map. if logically want term key, should have std::unordered_map<term, int>.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -