C++ 哈希表 (hashtable) 用于保存简单的数据,及数据查找,数据删除
/*hashtable.h*/ #include<iostream> #include <string> #include<vector> using namespace std; class Hashtable { protected: typedef pair<int,string> TIntStrPair; typedef pair<int, TIntStrPair> TIIntStrPair; typedef pair<string,string> TStrStrPair; typedef pair<string, TStrStrPair> TSStrStrPair; private: vector <TSStrStrPair> Table; // <string, <string, string>> ; public: int hashfunc(string s); string HashFunc(string value); //generate 20 位随机数 void add_hash_(string key, string); string search_(string value); void list_hash(); void print_hash(string hashIndex); bool delete_hash(string value); };
/*hashtable.cpp*/ #include "hashtable.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> using namespace std; int Hashtable::hashfunc(string s) { int i, sum=0; for(i=0; i < (int)s.size(); i++) sum=sum+s[i]; int result; result = (sum-1)%20; } string Hashtable::HashFunc(string value) { string x; char CCH[] = "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; char str[6]; char ss[21]; int i=0; static int count = 0; count++; //srand((char)(time((time_t *)NULL))); srand((char)(sizeof(value.c_str()) + count )); //srand((char)(sizeof(value.c_str()))); for (i = 0; i < 15; i++) { ss[i] = (char)(rand()%27 + 'a'); } ss[i] = '\0'; srand((char)(time((time_t *)NULL))); //srand((char)(sizeof(CCH))); for (i = 0; i < 5; i++) { int y = rand() / (RAND_MAX/(sizeof(CCH) - 1 + count )); str[i] = CCH[y]; } str[i] = '\0'; //printf("str = %s\n", str); strcat(ss,str); x = ss; return x; } void Hashtable::add_hash_(string key, string s) { std::pair<string,TStrStrPair> Temp; string hashIndex_; hashIndex_ = HashFunc(s); Temp.first = hashIndex_; Temp.second.first = key; Temp.second.second = s; Table.push_back(Temp); } string Hashtable::search_(string value) { string hashIndex; for (int x = 0; x < Table.size(); x++) { if (Table.at(x).second.second == value) { hashIndex = Table.at(x).first; cout << "Match " << value << " in the list" << endl; return hashIndex; } if (x == Table.size()) { cout << "Not match " << value << " in the list " << endl; //return hashIndex; } } return hashIndex; } void Hashtable::print_hash(string hashIndex) { if (hashIndex.size() == 0) { cout << "Not found in the list " << endl; return; } cout << "Hash Num: " << hashIndex << endl; for (int i = 0; i < Table.size(); i++) { if (Table.at(i).first == hashIndex) { cout << "Key = " << Table.at(i).second.first << endl; cout << "Value = " << Table.at(i).second.second << endl; } } } bool Hashtable::delete_hash(string value) { for (int x = 0; x < Table.size(); x++) { if (Table.at(x).second.second == value) { Table.erase(Table.begin() + x); return true; } } return false; } void Hashtable::list_hash() { if (Table.empty()) { cout << "There is not element in the list" << endl; return; } for (int x = 0; x < Table.size(); x++) { cout << "Hash Num: " << Table.at(x).first << endl;; cout << "Key: " << Table.at(x).second.first << endl; cout << "Value: " << Table.at(x).second.second << endl; cout << endl; } }
/*main.cpp*/ #include"hashtable.h" #include<iostream> int main() { string hashIndex; Hashtable ht; ht.add_hash_("1", "Tom"); //sleep(1); ht.add_hash_("2", "Mary"); //sleep(1); ht.add_hash_("3", "jimes"); ht.add_hash_("4", "fantex"); ht.add_hash_("5", "beyond"); ht.list_hash(); hashIndex = ht.search_("Mary"); ht.print_hash(hashIndex); cout << "delete element " << endl; ht.delete_hash("jimes"); ht.list_hash(); //ht.search_("Mary"); return 0; }
运行结果:
$ g++ -o new-t main.cpp hashtable.cpp Administrator@WIN7-20131114US /cygdrive/i/HashTable $ ./new-t Hash Num: ovbcmuxpydnpasuK2ibP Key: 1 Value: Tom Hash Num: xtsoaxrledvaxswL2jbQ Key: 2 Value: Mary Hash Num: rruaq{bwbnnxdtjM2jbR Key: 3 Value: jimes Hash Num: {pwmecmrznft{imN2jbR Key: 4 Value: fantex Hash Num: inyzdfwmvxyfgjpN2kbS Key: 5 Value: beyond Match Mary in the list Hash Num: xtsoaxrledvaxswL2jbQ Key = 2 Value = Mary delete element Hash Num: ovbcmuxpydnpasuK2ibP Key: 1 Value: Tom Hash Num: xtsoaxrledvaxswL2jbQ Key: 2 Value: Mary Hash Num: {pwmecmrznft{imN2jbR Key: 4 Value: fantex Hash Num: inyzdfwmvxyfgjpN2kbS Key: 5 Value: beyond
laser杨万荣