27

単純に使用できないことに少し驚いた

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

std::hashsの専門分野がないように思われるからですstd::array。何故ですか?それとも私は単にそれを見つけられませんでしたか?実際に何もない場合、次の実装の試みを簡略化できますか?

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;
        }
    };
}

これはどういうわけか標準ライブラリの一部であるべきだと私は本当に感じています。

4

2 に答える 2

12

答えではありませんが、いくつかの有用な情報です。C ++ 11標準の2月のドラフトでは、std::hashこれらのタイプに特化したものが指定されています。

  • error_code§19.5.5
  • bitset<N>§20.5.3
  • unique_ptr<T, D>§20.7.2.36
  • shared_ptr<T, D>§20.7.2.36
  • type_index§20.13.4
  • string§21.6
  • u16string§21.6
  • u32string§21.6
  • wstring§21.6
  • vector<bool, Allocator>§23.3.8
  • thread::id§30.3.1.1

そしてこれらすべてのタイプ:§20.8.12

template <> struct hash<bool>;
template <> struct hash<char>;
template <> struct hash<signed char>;
template <> struct hash<unsigned char>;
template <> struct hash<char16_t>;
template <> struct hash<char32_t>;
template <> struct hash<wchar_t>;
template <> struct hash<short>;
template <> struct hash<unsigned short>;
template <> struct hash<int>;
template <> struct hash<unsigned int>;
template <> struct hash<long>;
template <> struct hash<long long>;
template <> struct hash<unsigned long>;
template <> struct hash<unsigned long long>;
template <> struct hash<float>;
template <> struct hash<double>;
template <> struct hash<long double>;
template<class T> struct hash<T*>;
于 2012-01-06T22:07:53.743 に答える
11

標準ライブラリにこれが含まれていない理由はわかりませんが、Boostには、ハッシュ可能な型から構成されるあらゆる種類のものに対するハッシュがあります。このための重要な機能はhash_combine、からコピーすることを歓迎するですboost/functional/hash/hash.hpp

hash_combineBoostを使用して、 range_hash(範囲の各要素のハッシュを組み合わせるだけで)、ペアおよびタプルハッシャーを導出します。次にrange_hash、反復可能なコンテナをハッシュするために使用できます。

于 2011-11-06T16:34:01.353 に答える