Home > Back-end >  C hashmap how to use int and string type calculation hash_value () method all domain overall hash va
C hashmap how to use int and string type calculation hash_value () method all domain overall hash va

Time:09-25

 
Hash_map & lt; CTest, int, MyHashCompare> CHash;

Struct IHashable
{
Virtual unsigned long hash_value (const)=0;
Virtual Boolean operator & lt; (const IHashable& Val) const=0;
Virtual IHashable& Operator=(const IHashable& Val)=0;
};

The class CTest: public IHashable
{
Public:
Int m_value;
Cstrings m_message;
Public:
CTest () : m_value (0) {}

CTest (const CTest& Obj)
{
M_value=https://bbs.csdn.net/topics/obj.m_value;
M_message=obj. M_message;
}
Public:
Virtual IHashable& Operator=(const IHashable& Val) {
M_value=https://bbs.csdn.net/topics/(CTest & val). M_value;
M_message=((CTest&) Val). M_message;
Return (* this);
}

Virtual unsigned long hash_value () const {
//is used here in the class m_value domain to calculate the hash value, can also use more complex function to calculate all the domain general hash value
Return (m_value ^ 0 xdeadbeef);
}

Virtual Boolean operator & lt; (const IHashable& Val) const {
Return (m_value & lt; ((CTest&) Val) m_value);
}
};

TemplateThe class MyHashCompare: public hash_compare & lt; _Tkey & gt;
{
Public:
Size_t operator () (const _Tkey & amp; _Key) const {
Return (_Key hash_value ());
}


Boolean operator () (const _Tkey & amp; _Keyval1, const _Tkey & amp; _Keyval2) const {
Return (comp (_Keyval1 _Keyval2));
}
};


  • Related