efficient word dictionary implementation

Hello, I was wondering what would be the most efficient implementation of a dictionary (to store words and something about them), given that the most important feature is word lookup speed (ideally constant time).

My first thought was a hash table, but I have no idea what the hash function should be or how to implement a hash table. Is this a good implementation? if so, what would be a good hash function?

Any thoughts or suggestions would be greatly appreciated!
An easy way is using a std::map ( http://www.cplusplus.com/reference/stl/map/ ), it isn't a true hash table but it's easy to use.
The lookup speed (according to the reference page) is logarithmic
Very simple implementation of a hash map. You would need to greatly advance and change this. But here is a very simple idea of how to use one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <hash_map>

int main()
{
	typedef std::pair< std::string, std::string > PairWord;
	stdext::hash_map< std::string, std::string  > Dictionary;

	Dictionary.insert( PairWord("Run", "run run run its fun") );
	Dictionary.insert( PairWord("Walk", "walking is boring") );
	Dictionary.insert( PairWord("Jump", "jump for fun...") );

	// Search for a word in the dictionary
	std::string input = "";
	do
	{
		std::cout << "Look up a word\n\n  >> ";
		std::cin >> input;
		
		std::cout << Dictionary[input] << std::endl;
	}
	while( input != "exit" );

	//Display All the words.
	stdext::hash_map< std::string, std::string  >::iterator iter;
	for(iter = Dictionary.begin(); iter != Dictionary.end(); iter++)
	{
		std::cout << "Word : " << iter->first << std::endl;
		std::cout << "Meaning : " << iter->second << std::endl;
	}

	return 0;
}
Thanks for your help! I definitely want to use a hash map, since we need to look up words incredibly fast and do not want the log(n) factor (n is large).

To build this, I think I need a hash function which will take a word, and give me an index in an array containing the definitions (actually some other value). I am confused on what hash function will be useful; and how the has map will initially assign locations in the array. Any tutorials or suggestions on how to build hash maps?

Topic archived. No new replies allowed.