How to write qHash for a QSet container?

后端 未结 2 711
执笔经年
执笔经年 2020-12-17 23:06

I need to implement a set of sets in my application. Using QSet with a custom class requires providing a qHash() function and an operator==.

<
2条回答
  •  旧时难觅i
    2020-12-17 23:24

    A common way to hash containers is to combine the hashes of all elements. Boost provides hash_combine and hash_range for this purpose. This should give you an idea how to implement this for the results of your qHash.

    So, given your qHash for Custom:

    uint qHash(const QSet& c) {
      uint seed = 0;
    
      for(auto x : c) {
        seed ^= qHash(x) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
      }
    
      return seed;
    }
    

提交回复
热议问题