是否有任何的std ::哈希标准集装箱专业? [英] Are there no specializations of std::hash for standard containers?

查看:85
本文介绍了是否有任何的std ::哈希标准集装箱专业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<我一个href=\"http://stackoverflow.com/questions/8026890/c-how-to-insert-array-into-hash-set/8026914#8026914\">just发现自己有点吃惊暂时无法简单地用一个

I just found myself a little bit surprised being unable to simply use a

std::unordered_set<std::array<int, 16> > test;

,因为似乎没有成为 A 的std ::哈希专业化的std ::阵列秒。这是为什么?还是我根本就没有发现呢?如果确实是有没有,可以在下面的实现努力简化?

because there does not seem to be a std::hash specialization for std::arrays. Why is that? Or did I simply not find it? If there is indeed none, can the following implementation attempt be simplified?

namespace std
{
    template<typename T, size_t N>
    struct hash<array<T, N> >
    {
        typedef array<T, N> argument_type;
        typedef size_t result_type;

        result_type operator()(const argument_type& a) const
        {
            hash<T> hasher;
            result_type h = 0;
            for (result_type i = 0; i < N; ++i)
            {
                h = h * 31 + hasher(a[i]);
            }
            return h;
        }
    };
}

我真的觉得这应该是莫名其妙的标准库的一部分。

I really feel this should somehow be part of the standard library.

推荐答案

我不知道为什么标准库并没有包括这一点,但也加速了哈希各种从哈希类型组成的东西。这样做的主要功能是 hash_combine ,它欢迎您从升压/功能/散列/ hash.hpp复制

I'm not sure why the standard library hasn't included this, but Boost has hashing for all sorts of things made up from hashable types. The key function for this is hash_combine, which you are welcome to copy from boost/functional/hash/hash.hpp.

使用 hash_combine ,升压派生的 range_hash (只是结合众多的每个元素的哈希值)以及对和元组hashers。在 range_hash 又可以用于散列可迭代的容器。

Using hash_combine, Boost derives a range_hash (just combining the hashes of a each element of a range), as well as pair and tuple hashers. The range_hash in turn can be used to hash any iterable container.

这篇关于是否有任何的std ::哈希标准集装箱专业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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