Unverified Commit c34ed3e8 authored by XTh3G4p's avatar XTh3G4p Committed by GitHub
Browse files

Update unordered-container.md

增加 std::pair<int, int> 作为主键类型的哈希函数,内容仍旧来源于原博客。两种重载可以共存于同一个结构体 my_hash 中因为他们类型不同且不存在隐式转换。
parent 0fd28d33
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -54,7 +54,13 @@ struct my_hash {
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  
  // 针对 std::pair<int, int> 作为主键类型的哈希函数
  size_t operator()(pair<uint64_t,uint64_t> x) const {
	static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
	return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1);
  }
};
```

写完自定义的哈希函数后,就可以通过 `unordered_map<int, int, my_hash> my_map;` 的定义方式将自定义的哈希函数传入容器了。
写完自定义的哈希函数后,就可以通过 `unordered_map<int, int, my_hash> my_map;` 或者 `unordered_map<pair<int, int>, int, my_hash> my_pair_map;` 的定义方式将自定义的哈希函数传入容器了。