【问题标题】:How to reuse hashing function of string class for custom class in unordered map?如何在无序映射中为自定义类重用字符串类的散列函数?
【发布时间】:2012-03-24 03:17:22
【问题描述】:

我在 C++ 中定义了一个 unordered_map,如下所示:

unordered_map<CustomClass, int, CustomClassHash, CustomClassEq> myMap;

假设我已经能够成功定义 CustomClassEq。我想要 CustomClass 的哈希值。 CustomClassHash 委托给类内字符串属性的哈希值。有没有办法可以在CustomClassHash的定义中重用字符串类的散列函数?

这就是我想做的:

struct CustomClassHash {
    long operator()(const CustomClass &c) const {
        string s = c.getString();
        // TODO: return the hash of s
    }
};

【问题讨论】:

    标签: c++ hash tr1 unordered-map hash-function


    【解决方案1】:

    您可以使用: return hash&lt;string&gt;()(c.get_name());

    查看帖子的第二条评论

    object as a key of unordered map

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 2018-07-22
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 2021-06-15
      • 1970-01-01
      • 2021-12-24
      相关资源
      最近更新 更多