I'm trying to read in a list of words and save them in a C++ STL hash_map along with their position in the alphabetically sorted file. The idea is later I'll need to be able to tell if a string is a word and whether it comes before or after a different word.
ifstream f_dict ("dictionary.txt");
__gnu_cxx::hash_map <const char*, int> dictionary;
string temp_str;
int counter = 0;
while (!f_dict.eof()) {
f_dict >> temp_str;
dictionary.insert(make_pair(temp_str.c_str(), counter++));
}
The problem I'm having is that it isn't saving the actual word. The for loop
below prints out a selection of the words, but iter->first
is always empty. What am I missing?
__gnu_cxx::hash_map<const char*, int>::iterator iter;
int i = 0;
for (iter = dictionary.begin(); iter != dictionary.end() && i < 150; iter++) {
cout << "word: " << iter->first << " index: " << iter->second << "\n";
i++;
}