C++ 哈希表 (hashtable) 用于保存简单的数据,及数据查找,数据删除
2013-12-20 16:33
543 查看
/*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
参考文章:http://blog.csdn.net/jjiang06/article/details/6706134
相关文章推荐
- C++之一些事一些情--深拷贝和浅拷贝
- fstream和ifstream详细用法,c++文件输入输出要注意!!
- VC++6.0进度条的初级使用
- C++ 隐式类型转换与关键字explicit
- C++萃取技术的一个简单应用
- C++之一些事一些情--变量初始化
- Primer Plus C++中的预处理命令
- C++类里面的哪些成员函数是内联函数?
- c++ replaceAll
- c++ 备忘
- C++实现获取当前执行文件全路径
- switch入参:java中不可为long,C++中则可以
- C++乱七八糟
- 【C语言学习笔记】大端模式与小端模式
- 怎样用 C/C++ 开发一个原生程序运行在 Android 下而不用 ADK、NDK
- 使用Visual Studio 2010打造C语言编译器
- 【C语言学习笔记】指针的“加减”运算
- C++字符串点滴以及java字符串replace方法回顾 - sharpstill - 博客园
- Jenkins上的lint代码静态测试和代码风格检查cpplint
- C++手记--快速入门