散列指针作为C ++ STL中unordered_map的键 [英] Hashing pointers as Keys for unordered_map in C++ STL

查看:166
本文介绍了散列指针作为C ++ STL中unordered_map的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了一个类似的关于使用指针作为Keys on C ++ STL中的地图。用作Keys时,指针在unordered_maps中的散列方式如何。更具体地说,如果我定义:

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

默认的C ++ std :: hash实现是否可以处理这些指针?使用安全吗?这是一个很好的做法吗?

std :: hash< T *> 被定义,但它如何操作的细节取决于实现。它肯定是安全的使用,我认为这是一个好习惯 - 只要它是你需要的指针作为关键,而不是对象内容本身。


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的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆