string
struct hashfun
{
const int p = 31;
const int mo = 1e9 + 9;
size_t operator () (const string &ss) const
{
int hash_value = 0;
int p_pow = 1;
for(char c:ss)
{
hash_value = (hash_value + (c-'a'+1) * p_pow ) % mo;
p_pow = (p_pow * p) % mo;
}
return hash_value;
}
};
pair
struct hashfun
{
const int ma = 100000;
template<typename T, typename U>
size_t operator() (const pair<T, U> &i) const
{
return i.first*ma+i.second;
}
};
或
struct hashfun
{
template<typename T, typename U>
size_t operator() (const pair<T, U> &i) const
{
return hash<T>()(i.first) ^ hash<U>()(i.second);
}
};
long long 哈希
const int p1 = 998244353, p2 = 1e9 + 7, p3 = 1e9 + 9;
struct hashh{
size_t operator()(const int& xx)const
{
return (xx % p1) * (xx % p2) % p3;
}
};
unordered_map<int, int, hashh> mp;