当前位置:   article > 正文

重载unordered_map与unordered_set中的hash函数_unorderedset vector hash重载

unorderedset vector hash重载

在编程的过程中,hash_set和hash_map的key有时候可能需要是一些复杂的元素,如数对,结构体或者某一个类,这时候可能需要自己重新写一个hash函数来使得hash_set和hash_map能够正常使用。hash_set和hash_map的结构如下,只需要重载除key和value的下一个参数即可。

  1. template<class Key,
  2. class Ty,
  3. class Hash = std::hash<Key>,
  4. class Pred = std::equal_to<Key>,
  5. class Alloc = std::allocator<std::pair<const Key, Ty> > >
  6. class unordered_map;
  7. > class unordered_map
  8. template<class value,
  9. class Hash = std::hash<value>,
  10. class Pred = std::equal_to<value>,
  11. class Alloc = std::alloc>
  12. class unordered_set;

重写hashfun结构体的内容如下:要注意hash<int>()(a.first)这个格式。

  1. struct hashfun{
  2. size_t operator()(const pair<int,int>& a)const
  3. {
  4. return hash<int>()(a.first)*10+hash<int>()(a.second);
  5. }
  6. };
  7. unordered_set<pair<int,int>,hashfun> check;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/941312
推荐阅读
相关标签
  

闽ICP备14008679号