简体   繁体   中英

Hashing pointers as Keys for unordered_map in C++ STL

I posted a similar quetion regarding using pointers as Keys on maps in C++ STL. How are pointers hashed in unordered_maps when used as Keys. More specifically if I define:

std::unordered_map< CustomClass*, int > foo;

Would the default C++ std::hash implementation work to handle these pointers? Is it safe to use? Is this good practice?

std::hash<T*> is defined but the details of how it operates are implementation dependent. It will certainly be safe to use, and I'd consider it good practice - as long as it's the pointer you need as the key, and not the object contents itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM