#include <map>
#include <iostream>
static std::map<std::string,std::string> datamap;
/*
*@description: 存入一个string类型的数据
*@author: buerkai.iteye.com
*@fullName: put
*@param1: key
*@param2: value
*/
static void put(std::string key,std::string value)
{
if(datamap.find(key)!=datamap.end()){
datamap.find(key)->second=value;
}else{
datamap.insert(make_pair(key,value));
}
}
/*
*@description: 取一个string类型的数据,没有返回为空串
*@author: buerkai.iteye.com
*@fullName: get
*@param1: key
*/
static std::string get(std::string key)
{
if(datamap.find(key)!=datamap.end()){
return datamap.find(key)->second;
}else{
return "";
}
}
/*
*@description: 删除string类型的数据
*@author: buerkai.iteye.com
*@fullName: deleteValue
*@param1: key
*/
static void deleteValue(std::string key){
if(datamap.find(key)!=datamap.end()){
datamap.erase(key);
}
}
/*
*@description: 存入一个Int类型的数据
*@author: buerkai.iteye.com
*@fullName: put
*@param1: key
*@param2: value
*/
static void putInt(std::string key,int value)
{
char tt[10];
put(key,itoa(value,tt,10));
}
/*
*@description: 取一个Int类型的数据,没有返回0
*@author: buerkai.iteye.com
*@fullName: getInt
*@param1: key
*/
static int getInt(std::string key)
{
std::string dd=get( key);
if(dd==""){
return 0;
}else{
return std::atoi(dd.c_str());
}
}
/*
*@description: 存入一个Float类型的数据
*@author: buerkai.iteye.com
*@fullName: putFloat
*@param1: key
*@param2: value
*/
static void putFloat(std::string key,float value)
{
char tmpstr[32];
memset(tmpstr,'\0',32);
sprintf(tmpstr,"%f",value);
put(key,std::string(tmpstr));
}
/*
*@description: 取一个Float类型的数据,没有返回0.0f
*@author: buerkai.iteye.com
*@fullName: getFloat
*@param1: key
*/
static float getFloat(std::string key)
{
std::string dd=get( key);
if(dd==""){
return 0.0f;
}else{
return std::atof(dd.c_str());
}
}
/*
*@description: 存入一个bool类型的数据
*@author: buerkai.iteye.com
*@fullName: putBool
*@param1: key
*@param2: value
*/
static void putBool(std::string key,bool value)
{
std::string tt="0";
if(value)
{
tt="1";
}
put(key,std::string(tt));
}
/*
*@description: 取一个bool类型的数据,没有返回false
*@author: buerkai.iteye.com
*@fullName: getBool
*@param1: key
*/
static bool getBool(std::string key)
{
std::string dd=get( key);
if(dd==""||dd=="0"){
return 0;
}else if(dd=="1"){
return 1;
}
return 0;
}
前一段时间,在做小游戏的时候,将游戏的全局变量放在一个gamedata.h文件中,每次include这个文件的时候,里面定义的全局变量就重新初始化了,为了解决这个问题,简单模仿了一个JAVA中的hashtable。可以放在单列类中,存放游戏中的全局变量,以上函数已经过测试,放心使用吧。
转载请注明出处:http://buerkai.iteye.com
分享到:
相关推荐
开源项目-spion-hashtable-latencies.zip,Improved latency in Go's next version (1.8) at spion/hashtable-latencies
这是一个用c语言实现hashtable的例子, 里面适应折叠法实现散列函数,使用链表法处理冲突;
C语言实现 哈希表 11111111111111111111111111111111111111111111111111111111111111111111111111111111111
Hashtable的用法---马克-to-win Java视频哈希表的详细介绍
./a.out在本实验中,我们将实现一张地图。 为此,我们将使用在文件hashmap.c中找到的以下结构(哈希表) struct Pair { char * key; void * value;};struct HashMap { Pair ** buckets; long size; //cantidad de ...
哈希表 哈希表_使用C++实现的哈希表_HashTable
在本实验中,我们将实现一张地图。 为此,我们将使用在文件hashmap.c中找到的以下结构(哈希表) struct Pair { char * key; void * value; }; struct HashMap { Pair ** buckets; long size; //cantidad de ...
./a.out在本实验中,我们将实现一张地图。 为此,我们将使用在文件hashmap.c中找到的以下结构(哈希表) struct Pair { char * key; void * value;};struct HashMap { Pair ** buckets; long size; //cantidad de ...
./a.out在本实验中,我们将实现一张地图。 为此,我们将使用在文件hashmap.c中找到的以下结构(哈希表) struct Pair { char * key; void * value;};struct HashMap { Pair ** buckets; long size; //cantidad de ...
在本实验中,我们将实现一张地图。 为此,我们将使用在文件hashmap.c中找到的以下结构(哈希表) struct Pair { char * key; void * value; }; struct HashMap { Pair ** buckets; long size; //cantidad de ...
./a.out在本实验中,我们将实现一张地图。 为此,我们将使用在文件hashmap.c中找到的以下结构(哈希表) struct Pair { char * key; void * value;};struct HashMap { Pair ** buckets; long size; //cantidad de ...
该文档实现了Hashtable在C#中的常用的函数
WinFormHashTable最简单用法,.net hashtable ,hashtable ,hashtable用法
实现了链表法(Chaining)和开放地址寻址(Open Addressing)中的Hash表实现,开放地址寻址采用双重散列解决冲突
PLSQL-Hashtable 是使用对象语法用 PL/SQL 编写的哈希表实现。 哈希表是通用的,但可以专门化以提高速度。 对象类型可以持久地存储在 oracle 数据库中。
HashTable是在实际应用中很重要的一个结构,下面讨论一个简单的实现,虽然简单,但是该有的部分都还是有的。 一,访问接口 创建一个hashtable. hashtable hashtable_new(int size) /其中size表示包含的接点个数。...
一个简单的hash table实现类,实现了插入删除查询操作
哈希表 哈希表_使用C语言实现哈希表数据结构_HashTable
c语言Hashtable表的实现,和部分函数。仅供参考。
HashTable源码