unordered_setapp
unordered_mapless
template < class Key, // unordered_map::key_type class T, // unordered_map::mapped_type class Hash = hash<Key>, // unordered_map::hasher class Pred = equal_to<Key>, // unordered_map::key_equal class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type > class unordered_map;
方法1:函數
class InnerP2PString;ui
typedef std::tr1::shared_ptr<InnerP2PString> P2PString;this
class InnerP2PString {spa
public:code
InnerP2PString(const uint8_t *val, uint16_t len);string
InnerP2PString(const std::string &val);hash
~InnerP2PString();it
std::string val_;
uint32_t use_times_;
};
typedef std::tr1::unordered_set<P2PString> FileMap;
typedef std::tr1::unordered_map<P2PString, uint32_t> FileTracks;
如今使用的類是P2PString,做爲hash_map主鍵,須要重載一下操做符
namespace std {
namespace tr1 {
template<>
struct hash<P2PString>
: public std::unary_function<std::string, std::size_t> {
std::size_t
operator()(const P2PString& s) const {
// return Fnv_hash<>::hash((char*)s->val_.data(), s->val_.length());
const char* first = s->val_.data();
std::size_t length = s->val_.length();
std::size_t result = static_cast<std::size_t>(14695981039346656037ULL);
for (; length > 0; --length) {
result ^= (std::size_t)*first++;
result *= 1099511628211ULL;
}
return result;
}
};
}
template <>
struct equal_to<P2PString> : public binary_function<P2PString, P2PString, bool>
{
bool
operator()(const P2PString& __x, const P2PString& __y) const
{ return __x->val_ == __y->val_; }
};
template <>
struct less<P2PString> : public binary_function<P2PString, P2PString, bool>
{
bool
operator()(const P2PString& __x, const P2PString& __y) const
{ return __x->val_ < __y->val_; }
};
}
方法2:
struct KeyPidInfo
{
char key[32];
int pid;
bool operator<(const KeyPidInfo &keyPid) const
{
int flag = strcmp(this->key, keyPid.key);
return (flag < 0) ||
((flag == 0) && (this->pid < keyPid.pid));
}
};
struct Hash_KeyPidInfo
{
size_t operator()(const KeyPidInfo &keyPidInfo) const
{
char tmp[42];
int len = strlen(keyPidInfo.key);
strcpy(tmp, keyPidInfo.key);
string pid;
pid = lexical_cast<string>(keyPidInfo.pid);
strcpy(tmp + len, pid.c_str());
hash<char *> ht;//直接用了已經提供的hah函數,沒有本身編寫hash
return ht(tmp);
}
};
struct Equal_KeyPidInfo
{
bool operator()(const KeyPidInfo &left, const KeyPidInfo &right) const
{
return (strcmp(left.key, right.key) == 0) && (left.pid == right.pid);
}
};
用的時候這樣定義:
unordered_map<KeyPidInfo, int, Hash_KeyPidInfo, Equal_KeyPidInfo> valueMap;
備註:
class Thing {
public:
bool operator== (const Thing&) const;
};
struct Hash_Thing {
// hash function object class for Thing
std::size_t operator() (const Thing& t) const
{ /* compute and return a size_t value using some property of Thing */}
};
unordered_set<Thing, Hash_Thing> uset_of_Things;
std::hash_map
須要定義仿函數結構體
struct StringHashFunc //hash 函數
{
size_t operator() (const string& str)const
{
return __stl_hash_string(str.c_str());
};
};
struct StrCompare //判斷是否相等, 兩個值hash統一位置,須要調用此函數判斷主鍵是否存在
{
bool operator()(const string& str, const string& str1) const
{
return strcmp(str.c_str(), str1.c_str())==0;
}
};
typedef hash_set<string, StringHashFunc, StrCompare> T_StrEqueHashSet;