Map" int Triplets"到int?

时间:2012-03-22 01:10:55

标签: c++ hash std unordered-map

使用c ++ std的unordered_map我想将整数三元组映射到一个整数,我通常不使用哈希表(不知道它们太酷了),但我不知道在这种情况下正确的方法,使用默认的散列函数,我应该直接映射三元组(类似于<< int,int>,int> - > int)

std::unordered_map <std::make_pair <make_pair <int,int>,int>,int> hash;

或者可能使用函数将三元组映射到单个值并将该值与默认函数一起使用?

int mapping(int a, int b, int c){
}

std::unordered_map <int,int> hash;
这两种方法都有效,但我想知道哪种方法效率最高。谢谢

3 个答案:

答案 0 :(得分:3)

首先,您可以使用std::tuple<int, int, int>作为密钥类型。

接下来,您需要一种方法来散列元组,因为您可以散列每个元素。在Boost中有一个名为hash_combine的功能可以做到这一点,但由于我不清楚的原因,标准中没有包含这个功能。无论如何,这里是:

#include <tuple>
#include <utility>

template <class T>
inline void hash_combine(std::size_t & seed, const T & v)
{
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

template <class Tuple, std::size_t Index = std::tuple_size<Tuple>::value - 1>
struct tuple_hash_impl
{
    static inline void apply(std::size_t & seed, Tuple const & tuple)
    {
        tuple_hash_impl<Tuple, Index - 1>::apply(seed, tuple);
        hash_combine(seed, std::get<Index>(tuple));
    }
};

template <class Tuple>
struct tuple_hash_impl<Tuple, 0>
{
    static inline void apply(std::size_t & seed, Tuple const & tuple)
    {
        hash_combine(seed, std::get<0>(tuple));
    }
};

namespace std
{
    template<typename S, typename T> struct hash<pair<S, T>>
    {
        inline size_t operator()(const pair<S, T> & v) const
        {
            size_t seed = 0;
            ::hash_combine(seed, v.first);
            ::hash_combine(seed, v.second);
            return seed;
        }
    };

    template<typename ...Args> struct hash<tuple<Args...>>
    {
        inline size_t operator()(const tuple<Args...> & v) const
        {
            size_t seed = 0;
            tuple_hash_impl<tuple<Args...>>::apply(seed, v);
            return seed;
        }
    };
}

答案 1 :(得分:1)

“效率最高”似乎取决于你的编译器,但我会说make_pair解决方案看起来像一团糟。更好地使用你自己的哈希函数...只要确保你组成一个体面的哈希函数:)

答案 2 :(得分:0)

使用一对配对的解决方案应该非常有效。就哈希而言,很难将三个整数映射到更简单的东西。

相关问题