Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m working on a translator in C++. Basically I want to parse the file with tra

ID: 654333 • Letter: I

Question

I'm working on a translator in C++. Basically I want to parse the file with translations and store it in my program, so I can perform search through the words and simply access the corresponding word. My file will look like that:

word|translation
second word|second translation
etc. It doesn't have to be | as delimiter and the word can contain spaces. So after I store it in my program I want to search for a word and get the corresponding word easily.

The question is, what is 'the best' way to store this dictionary? Should I use dynamic structures and link them? Maybe vectors? Or should I use two-dimensional array to store the 2 strings? Could you please propose to me how the structure will look like?

Explanation / Answer

Since you're going to search by the first word, I'd suggest using a Hashmap.

A Hashmap is designed to solve exactly this issue: Search for a complicated key; It's also sometimes referred to as "Dictionary," so you know it's about this. It works by defining a function (Which is called "hash function") from the key-domain ("word" in your dictionary) to int, and then use these ints as the position in an array, where it stores both original key and value ("word" and "translation"). If your input is identical to some key, then the result of the hash function will give you the right int key, and you can complete your search very fast.