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函数有很多方法,本文用除留余数法. 除留余数法的概念: 取一个固定的基数的余数,注意不能用偶数,用偶数的话,分布会不 ...
随机推荐
- hdu Crazy Circuits
Crazy Circuits 题目: 给出一个电路板,从+极出发到负极. 如今给你电路板上的最小电流限制,要你在电流平衡的时候求得从正极出发的最小电流. 算法: 非常裸的有源汇最小流.安有源汇最大流做 ...
- JMS的样例
1.JMS是一个由AS提供的Message服务.它能接受消息产生者(Message Provider)所发出的消息,并把消息转发给消息消费者(Message Consumer).2.JMS提供2种类 ...
- ASP.NET MVC的跳转攻击问题
在ASP.NET MVC的自带的模板代码中,有这样一段,用来拦截非登录用户,使其跳转到登录页面,然后登录后在跳转回原页面.所以,期间有一个returnUrl参数用来保存原页面地址.在Login Act ...
- 自编Ps教程—我的ps图片赞赏
上篇讲述了主要的ps概念和操作,这里不再讲述了,主要的操作学好了,其它的都简单,下面我会把我闲暇时间天马行空的小作品上穿,以供大家闲暇时间或者工作累了的时候赞赏! 以后还会在这里上传哦!喜欢就收藏吧! ...
- c++一些语法模板
函数模板特 template <class T> int compare(T v1,T v2) { if(v1<v2) return -1; else if(v1>v2) re ...
- Node.js: What is the best "full stack web framework" (with scaffolding, MVC, ORM, etc.) based on Node.js / server-side JavaScript? - Quora
Node.js: What is the best "full stack web framework" (with scaffolding, MVC, ORM, etc.) ba ...
- cocos2d-x 消类游戏,类似Diamond dash 设计
前几天刚刚在学习cocos2d-x,无聊之下自己做了一个类似Diamond dash的消类游戏,今天放到网上来和大家分享一下.我相信Diamond dash这个游戏大家都玩过,游戏的规则是这样的,有一 ...
- Linux mysql 数据库忘记root密码
1.修改MySQL的登录设置: # vi /etc/my.cnf 1在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi. 2.重新启动mysqld # /etc/i ...
- gwt CellTable中的控件按Tab键切换
默认是 cellTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); 如果要Tab,则设置为DISABLED; 并将其t ...
- 用java读写properties文件的代码
package com.LY; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.F ...