practice1.h(包含线性映射)
#ifndef PRACTICE1_H_INCLUDED#define PRACTICE1_H_INCLUDED#includetemplate class LinerMap //线性映射{ public: LinerMap(int size=101):arr(size) { currentSize=0; } void Put(const Key&k,const Value & v) { arr[currentSize]=DataEntry(k,v); currentSize+=1; } Value Get(const Key & k) { //线性查找 for(size_t i=0;i arr; int currentSize;};#endif // PRACTICE1_H_INCLUDED
hashmap.h文件
#ifndef HASHMAP_H_INCLUDED#define HASHMAP_H_INCLUDED#includetemplate class HashMap //哈希映射{ public: HashMap(int size=101):arr(size) { currentSize=0; } void Put(const Key&k,const Value & v) { int pos=myhash(k); arr[pos]=DataEntry(k,v); ++currentSize; } Value Get(const Key & k) { int pos=myhash(k); if(arr[pos].key==k) return arr[pos].value; else return Value(); } unsigned hash(const Key & k) const { unsigned int hashVal=0; const char *keyp=reinterpret_cast (&k);//转换成字符 for (size_t i=0;i arr; int currentSize;};#endif // PRACTICE1_H_INCLUDED
practice.cpp文件
#include#include