2013-01-23 60 views

回答

0
class PtrStr 
{ 
    protected: 
     const char *m_pcStr; 
     const int m_nSize; 

    public: 
     PtrStr(const char *pcStr, int nSize) : m_pcStr(pcStr), m_nSize(nSize) {} 
     ~PtrStr() {}; 

     const char *GetStr() const { return m_pcStr; } 
     const int GetSize() const { return m_nSize; } 
}; 

namespace std 
{ 
    namespace tr1 
    { 
     template<> struct hash<PtrStr> 
     { 
      std::size_t operator()(PtrStr const &key) const 
      { 
       return MyHashMethod(key); 
      } 
     }; 
    } 

    template<> struct equal_to<PtrStr> 
    { 
     bool operator()(const PtrStr &keyA, const PtrStr &keyB) const 
     { 
      return (strcmp(const_cast<PtrStr &>(keyA).GetStr(), const_cast<PtrStr &>(keyB).GetStr()) == 0); 
      return (strcmp(keyA.GetStr(), keyB.GetStr()) == 0); 
     } 
    }; 
} 

typedef std::tr1::unordered_map<PtrStr, int> unorderedCharMap; 
typedef std::tr1::unordered_map<PtrStr, int>::iterator unorderedCharMapItr; 
+0

注意一个很好的散列函数是SuperFastHash:http://www.azillionmonkeys.com/qed/hash.html –