自定义类型hash

 1 struct LogEventKey
 2 {
 3     LogEventId nId = 0;   // 事件Id
 4     LogEventTag nTag = 0; // 事件Tag
 5 
 6     bool operator==(const LogEventKey& rhs) const {
 7         return nId == rhs.nId && nTag == rhs.nTag;
 8     }
 9     bool operator<(const LogEventKey& rhs) const {
10         return nId < rhs.nId || (nId == rhs.nId && nTag < rhs.nTag);
11     }
12     bool operator<=(const LogEventKey& rhs) const {
13         return (*this == rhs) || (*this < rhs);
14     }
15     bool operator>(const LogEventKey& rhs) const {
16         return !(*this <= rhs);
17     }
18     bool operator>=(const LogEventKey& rhs) const {
19         return !(*this < rhs);
20     }
21     bool operator!=(const LogEventKey& rhs) const {
22         return !(*this == rhs);
23     }
24     
25 };//struct LogEventKey
26 
27 
28 namespace std {
29 
30     template<>
31     struct hash<LogEventKey>
32     {
33         std::size_t operator()(const LogEventKey& key) const {
34             using std::size_t;
35             using std::hash;
36             using std::string;
37 
38             // Compute individual hash values for first,
39             // second and third and combine them using XOR
40             // and bit shifting:
41 
42             return ((hash<LogEventId>()(key.nId)
43                    ^ (hash<LogEventTag>()(key.nTag) << 1)) >> 1);
44         }
45     };
46 
47 }
=== [Author: jacc.kim] & [e-mail: 175246359@qq.com] ===
原文地址:https://www.cnblogs.com/tongy0/p/15046143.html