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_("", "Tom");
//sleep(1);
ht.add_hash_("", "Mary");
//sleep(1);
ht.add_hash_("", "jimes");
ht.add_hash_("", "fantex");
ht.add_hash_("", "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 ;
}
运行结果:
$ 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++ 哈希表 (hashtable) 用于保存简单的数据,及数据查找,数据删除的更多相关文章
- 哈希表(hashtable)的javascript简单实现
javascript中没有像c#,java那样的哈希表(hashtable)的实现.在js中,object属性的实现就是hash表,因此只要在object上封装点方法,简单的使用obejct管理属性的 ...
- 哈希表 HashTable(又名散列表)
简介 其实通过标题上哈希表的英文名HashTable,我们就可以看出这是一个组合的数据结构Hash+Table. Hash是什么?它是一个函数,作用可以通过一个公式来表示: index = HashF ...
- C# 哈希表HashTable的简单使用
本人C#程序菜鸟级别的存在,写博客一方面是为了知识的共享,另一方面也是为了督促自己:大神,可以忽略这篇文文的.废话到此...... 哈希表是可以直接进行访问的数据结构,在形式上是类似字典的.不同的是, ...
- 哈希表(Hashtable)简述
一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,其中 ...
- [PHP] PHP数组的实现哈希表(HashTable)结构
PHP中使用最为频繁的数据类型非字符串和数组莫属,使用哈希表实现的PHP数组.1.数据结构:保存哈希表容器,保存数据的容器2.哈希函数实现:需要尽可能的将不同的key映射到不同的槽(bucket)中, ...
- C#中哈希表(HashTable)的用法详解以及和Dictionary比较
1. 哈希表(HashTable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对, ...
- Java中哈希表(Hashtable)是如何实现的
Java中哈希表(Hashtable)是如何实现的 Hashtable中有一个内部类Entry,用来保存单元数据,我们用来构建哈希表的每一个数据是Entry的一个实例.假设我们保存下面一组数据,第一列 ...
- 转 C#中哈希表(HashTable)的用法详解
看了一遍有关哈希表的文字,作者总结的真是不错 .收藏起来 1. 哈希表(HashTable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提 ...
- c/c++ 哈希表 hashtable
c/c++ 哈希表 hashtable 概念:用key去查找value 实现hash函数有很多方法,本文用除留余数法. 除留余数法的概念: 取一个固定的基数的余数,注意不能用偶数,用偶数的话,分布会不 ...
随机推荐
- 腾讯測试project师笔试面试记录
从3月29日參加腾讯笔试開始,開始了为期1周的腾讯之旅,尽管最后还是跪在了二面上,可是感觉收获非常多,至少明确了自己与向往的BAT公司的差距,明确了自己还是路漫漫其修远兮. 腾讯非常注 ...
- dtach-linux-分离功能-小工具 - 点点滴滴 Linux | 点点滴滴 Linux
dtach-linux-分离功能-小工具 - 点点滴滴 Linux | 点点滴滴 Linux dtach-linux-分离功能-小工具 2013年05月20日 ⁄ Linux工具 ⁄ 共 1775字 ...
- JavaScript2谁刚开始学习应该知道4最佳实践文章(翻译)
原版的:24 JavaScript Best Practices for Beginners (注:阅读原文的时候没有注意公布日期,觉得不错就翻译了,翻译到JSON.parse那一节觉得有点不正确路才 ...
- 解决SQL查询总是超时已过期
解决SQL查询总是超时已过期 .在WIN8里提示:OLE DB 或 ODBC 错误 : 查询超时已过期; HYT00 1.由于数据库设计问题造成SQL数据库新增数据时超时 症状: Microso ...
- 拿到阿里,网易游戏,腾讯,smartx的offer的过程 (转)
前言 从今年的3月14日阿里的电话面试开始,到现在4月16日在西安悦豪酒店进行的腾讯HR面到现在一个多月了,中间先后收到了阿里,网易游戏,腾讯和smartx的offer,今天早晨刚刚接到了腾讯HR的电 ...
- Extjs 3.4 和 web SSH(Ajaxterm)-howge-ChinaUnix博客
Extjs 3.4 和 web SSH(Ajaxterm)-howge-ChinaUnix博客 Extjs 3.4 和 web SSH(Ajaxterm) 2013-04-07 15:20:17 ...
- Wix打包系列(五) 部署数据库
原文:Wix打包系列(五) 部署数据库 很多人在使用vs进行打包的时候,经常会为数据库部署的问题犯愁,即便是重写Installer类的方法,也不是很可靠方便,下面我们来看看在wix中如何部署数据库. ...
- ORA-00210 ORA-15001 ORA-15055 ORA-01031: insufficient privileges
ORA-00210: cannot open the specified control file ORA-00202: control file: '+DATA/posdb/con ...
- redis作为mysql的缓存服务器(读写分离) (转)
一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...
- coco2d-x CCScrollView实现背包翻页,仅供参考
#include "CCCGameScrollView.h" USING_NS_CC; USING_NS_CC_EXT; CCCGameScrollView::CCCGameScr ...