Reference :
http://mikecvet.wordpress.com/2011/01/28/customizing-tr1-unordered_map-hashing-and-equality-functions/
http://en.cppreference.com/w/cpp/utility/hash
http://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key
http://mikecvet.wordpress.com/2011/01/28/customizing-tr1-unordered_map-hashing-and-equality-functions/
http://en.cppreference.com/w/cpp/utility/hash
http://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key
template<class Key, class T, class Hash = hash<Key>, class Pred = std::equal_to<Key>, class Alloc = std::allocator<std::pair<const Key, T> > >typedef struct{ long operator() (const AggregateKey &k) const { return my_hash_fnct (k); }} AggregateKeyHash;typedef struct{ bool operator() (const AggregateKey &x, const AggregateKey &y) const { return my_eq_test (x, y); }} AggregateKeyEq;AggregateKey k;{...}// Now, hash value generation and equality testing are// defined for the AggregateKeyHash type, so declare the// map using the functors above in the object's template liststd::tr1::unordered_map<AggregateKey, int, AggregateKeyHash, AggregateKeyEq> M;M[k] = 1;{...}std::tr1::unordered_map<int, int> M;const std::tr1::unordered_map<int, int>::hasher &hfn = M.hash_function ();const std::tr1::unordered_map<int, int>::key_equal &eqfn = M.key_eq ();long h = hfn (123);bool b = eqfn (1, 2); |
struct Key{
|
struct Key{
|
No comments:
Post a Comment