The principle is reserved.
Simple HashMap class Definition:
1#include <iostream>2 using namespacestd;3 classMysimplehashmap4 {5 Public:6 int*Array;7 enum{maxhashsize=7};8 9 intHashcode (int&key);Ten One Public: A Mysimplehashmap (); -~Mysimplehashmap (); - the int&operator[](int&key); - - }; - + - int& Mysimplehashmap::operator[](int&key) + { A returnArray[hashcode (key)]; at } - - - Mysimplehashmap::mysimplehashmap () - { -array=New int[maxhashsize]; in - for(intI=0; i<maxhashsize;i++) to { +array[i]=0; - } the } * $mysimplehashmap::~Mysimplehashmap ()Panax Notoginseng { - Delete[] Array; thearray=NULL; + } A the intMysimplehashmap::hashcode (int&key) + { - while(true) $ { $ if(array[key%maxhashsize]==0) - { - returnkey%maxhashsize; the } - ElseWuyi { thekey++; - } Wu } -}
Main.c
1#include"VpoetHashmap.h"2#include <iostream>3 using namespacestd;4 5 intMain ()6 {7 Mysimplehashmap Myhash;8 9 intkey1= the;//Hashkey=1Ten intKey2= -;//hashkey=2 One intkey3=9;//hashkey=2 A intkey4= One;//hashkey=4 Conflict - intkey5=5;//hashkey=5 - themyhash[key1]= -; -myhash[key2]= $; -myhash[key3]= -; -myhash[key4]= -; +myhash[key5]= -; - +cout<<"Hash Table Node1:"<<myhash.array[0]<<Endl; Acout<<"Hash Table Node2:"<<myhash.array[1]<<Endl; atcout<<"Hash Table Node3:"<<myhash.array[2]<<Endl; -cout<<"Hash Table node4:"<<myhash.array[3]<<Endl; -cout<<"Hash table Node5:"<<myhash.array[4]<<Endl; -cout<<"Hash Table Node6:"<<myhash.array[5]<<Endl; -cout<<"Hash Table Node7:"<<myhash.array[6]<<Endl; - return 0; in}
Run:
Simple implementation of hash table principle and HashMap