简体   繁体   English

将指针散列为C ++ STL中unordered_map的键

[英]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. 我张贴了类似的quetion使用在C ++ STL地图指针作为键就。 How are pointers hashed in unordered_maps when used as Keys. 当用作Keys时,指针如何在unordered_maps中进行哈希处理。 More specifically if I define: 更具体地说,如果我定义:

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

Would the default C++ std::hash implementation work to handle these pointers? 默认的C ++ std :: hash实现是否可以处理这些指针? Is it safe to use? 使用安全吗? Is this good practice? 这是好习惯吗?

std::hash<T*> is defined but the details of how it operates are implementation dependent. std::hash<T*>已定义,但其操作细节依赖于实现。 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. 它肯定是安全的,我认为这是一个好习惯 - 只要它是你需要的指针作为键,而不是对象内容本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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