当前位置:   article > 正文

C++ hash模板_c++hash模板

c++hash模板
  1. #include <iostream>
  2. #include <unordered_set>
  3. #include <string>
  4. #include <functional>
  5. using namespace std;
  6. class Customer {
  7. public:
  8. std::string fname, lname;
  9. double sour;
  10. Customer(std::string fn, string ln, double s)
  11. :fname(fn), lname(ln), sour(s)
  12. {}
  13. };
  14. //用来计算Hash
  15. template <typename Type>
  16. inline void hash_combine(std::size_t& seed,const Type& val) {
  17. seed ^= hash<Type>()(val) +
  18. 0x9e3779b9 +
  19. (seed << 6) +
  20. (seed >> 2);
  21. }
  22. template <typename Type>
  23. inline void hash_val(std::size_t& seed, const Type& val) {
  24. hash_combine(seed, val);
  25. }
  26. template <typename t1, typename... Type>
  27. inline void hash_val(std::size_t& seed, const t1& val, Type& ... args) {
  28. hash_combine(seed, val);
  29. hash_val(seed, args...);
  30. }
  31. template <typename... Type>
  32. inline size_t hash_val(const Type& ... args) {
  33. std::size_t seed = 0;
  34. hash_val(seed, args...);
  35. return seed;
  36. }
  37. struct Hash_ {
  38. public:
  39. std::size_t operator()(const Customer& val)const {
  40. return hash_val(val.fname, val.lname, val.sour);
  41. }
  42. };
  43. int main() {
  44. const Customer pp("111","2222,",52.0);
  45. cout << Hash_()(pp) << endl;
  46. const Customer pp1("111", "asdfasdfasdfas,", 52.0);
  47. cout << Hash_()(pp1) << endl;
  48. const Customer pp2("adsfasdfa", "2222,", 52.0);
  49. cout << Hash_()(pp2) << endl;
  50. return 0;
  51. }

 

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

闽ICP备14008679号