本文介绍了散列指针作为C ++ STL中unordered_map的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发布了一个类似的被定义,但它如何操作的细节取决于实现。它肯定是安全的使用,我认为这是一个好习惯 - 只要它是你需要的指针作为关键,而不是对象内容本身。
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.
这篇关于散列指针作为C ++ STL中unordered_map的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!