【问题标题】:The C++ Standard doesn`t provide a hash for this typeC++ 标准不提供这种类型的散列
【发布时间】:2017-05-30 12:52:54
【问题描述】:

我正在尝试使用一对作为键来创建无序映射。这对我来说是新事物,所以我遵循了一些教程并写了这个:

    struct pair_hash {
        template <class T1, class T2>
        std::size_t operator () (const std::pair<T1, T2> &p) const {
            auto h1 = std::hash<T1>{}(p.first);
            auto h2 = std::hash<T2>{}(p.second);

            return h1 ^ h2;
        }
    };

int wmain(int argc, wchar_t * argv[])
{
   {...}

    using Key = std::pair<DWORD, DWORDLONG>;
    std::unordered_map<Key, USN, pair_hash> mymap;


    std::pair<DWORD, DWORDLONG> mypair(dwVolSN, fileId);

    mymap.insert({ mypair, usn });

    std::unordered_map<Key, USN>::const_iterator got;
    got = mymap.find(mypair); // HERE I GET THE ERROR

    return 0

}

【问题讨论】:

  • 你有什么问题?
  • 我不知道如何将 mymap.find(mypair) 结果传递给“got”,我在构建时收到以下错误:C++ 标准不提供此类型的哈希跨度>

标签: c++ c++11 msdn unordered-map


【解决方案1】:

试试std::unordered_map&lt;Key, USN,pair_hash&gt;::const_iterator got;

auto got = mymap.find(mypair);

【讨论】:

  • 谢谢!这就是问题所在,我没有看到我忘记了“pair_hash”!我被困在这里1小时!谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2022-10-13
  • 2014-06-08
相关资源
最近更新 更多