如何扩展std :: tr1 :: hash自定义类型? [英] How to extend std::tr1::hash for custom types?

查看:275
本文介绍了如何扩展std :: tr1 :: hash自定义类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何允许STL实现提取我的自定义类型?在MSVC上,有一个类 std :: tr1 :: hash ,我可以使用

How do I allow the STL implementation to pick up my custom types? On MSVC, there is a class std::tr1::hash, which I can partially specialize by using

namespace std 
{
    namespace tr1 
    { 
        template <> 
        struct hash<MyType> 
        { ... };
    } 
}

但这是推荐的方法吗?此外,这是否与GCC的实现一起使用?对于 boost :: hash ,足以提供一个自由函数 size_t hash_value(const MyType&)类似的TR1实现?

but is this the recommended way? Moreover, does this work with GCC's implementation as well? For boost::hash, it's enough to provide a free function size_t hash_value (const MyType&), is there something similar for the TR1 implementation?

推荐答案

是的,这也将适用于GCC。我在一个更大的项目中使用它,它的工作没有问题。你也可以为TR1容器提供你自己的自定义哈希类,但是它指定std :: tr1 :: hash<>是默认的哈希类。专门用于自定义类型似乎是扩展标准哈希函数的自然方式。

Yes, this will also work for GCC. I'm using it in a bigger project and it works without problems. You could also provide your own custom hashing class for the TR1 containers, but it is specified that std::tr1::hash<> is the default hashing class. Specializing it for custom types seems like the natural way to extend the standard hashing functionality.

这篇关于如何扩展std :: tr1 :: hash自定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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