RT,某一天突发奇想写hash模板,结果挂了
#include <bits/stdc++.h>
using namespace std;
namespace __hash_table{
const int mod = 1e6+3;
const int base = (1<<19)-1;
int random_num;
typedef long long LL;
typedef unsigned long long ULL;
int hash(int num){
return abs(num%mod);
}
int hash(LL num){
return abs(num%mod);
}
int hash(short num){
return abs(num);
}
int hash(unsigned short num){
return num;
}
int hash(ULL num){
return num%mod;
}
int hash(unsigned num){
return num%mod;
}
int hash(const string& str){
unsigned int ans = 0;
const int len = str.size();
for(int i = 0;i < len;i ++ )ans = (ans*131+(str[i]-'0'+1))%mod;
return ans;
}
int hash(const char* str){
unsigned int ans = 0;
const int len = strlen(str);
for(int i = 0;i < len;i ++ )ans = (ans*131+(str[i]-'0'+1))%mod;
return ans;
}
int hash(double num){
static bool iscalled = false;
if(!iscalled){
srand(time(NULL));
random_num = rand()%1331+1;
iscalled = true;
}
ULL temp = num*(double)mod*(double)131*(double)random_num;
return temp%base;
}
template<class key,class vaule,vaule zero>
class hash_table{
public:
struct hash_class{
vaule v;
key k;
};
vector<hash_class> table[mod];
vaule& operator[](key KEY){
int ptr = hash(KEY);
const int len = table[ptr].size();
for(int i = 0;i < len;i ++){
if(table[ptr][i].k == KEY){
return table[ptr][i].v;
}
}
table[ptr].push_back(hash_class{zero,KEY});
return table[ptr].back().v;
}
};
}
int main() {
__hash_table::hash_table <const char*,int,0> month;
month["一月"] = 1,month["二月"] = 2;
month["三月"] = 3,month["四月"] = 4;
month["五月"] = 5;month["六月"] = 6;
month["八月"] = 8;month["七月"] = 7;
month["九月"] = 9;month["十二月"] = 12;
month["十月"] = 10;month["十一月"] = 11;
cout << month["十二月"] << endl;
return 0;
}