[英]C++ Read File Into hash_map
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. 我正在尝试阅读单词列表,并将它们在字母排序文件中的位置以及它们的位置保存在C ++ STL hash_map中。 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. 下面的
for loop
打印出单词的选择,但是iter->first
始终为空。 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++;
}
You are trying to store the same const char * for each word because your never creating any new memory for the word pulled from the file. 您试图为每个单词存储相同的const char *,因为您从未为从文件中提取的单词创建任何新的内存。 If you print out the pointer being returned from
temp_str.c_str()
, it will be the same for every call within your first loop. 如果打印出从
temp_str.c_str()
返回的指针,则在第一个循环中的每个调用都将是相同的。 In your second loop you're printing out the same char * for every record in your map (note there is only 1 b/c map does not allow dups) which has been set to empty string either within the 1st loop or between that and your for loop. 在第二个循环中,您将为映射中的每个记录打印相同的char *(请注意,只有1个b / c映射不允许重复),在第一个循环中或在第一个循环之间已将其设置为空字符串您的for循环。
Here is example code that demonstrates the problem and a solution. 这是演示该问题和解决方案的示例代码。
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
int main (int argc, char **argv)
{
ifstream file("test.txt");
map<const char *, int> dictionary;
map<string, int> strDictionary;
string temp_str;
int counter = 0;
while (!file.eof())
{
file >> temp_str;
cout << "PARSED: " << temp_str << "\n";
cout << "INSERTING: " << (unsigned long) temp_str.c_str() << "\n";
dictionary.insert(make_pair(temp_str.c_str(), counter));
strDictionary.insert(make_pair(temp_str, counter));
counter++;
}
cout << "Dictionary Size: " << dictionary.size() << "\n";
cout << "Str Dictionary Size: " << strDictionary.size() << "\n";
for (map<const char*, int>::const_iterator iter = dictionary.begin();
iter != dictionary.end();
++iter)
{
cout << "CHAR * DICTINARY: " << iter->first << " -> " << iter->second << "\n";
}
for (map<string, int>::const_iterator iter = strDictionary.begin();
iter != strDictionary.end();
++iter)
{
cout << "STR DICTIONARY: " << iter->first << " -> " << iter->second << "\n";
}
return 1;
}
您想使用std :: string作为键类型,而不是const char *,否则该字符串将不会被复制,并且最终在每次插入时都使用相同的键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.