带有指针值的C++映射
C++ map with pointer value
我尝试读取一个字典文件,其中每一行都包含单词id、单词和频率,用空格分隔。问题是,用来存储单词的地图具有相同的值。如果你能帮我,我非常感激。
typedef struct{
int id;
int count;
char* word;
} WORD;
//read file
std::map<int, WORD*> readWordMap(char* file_name)
{
std::ifstream infile(file_name, std::ifstream::in);
std::cout<<"word map read file:"<<file_name<<std::endl;
if (! infile) {
std::cerr<<"oops! unable to open file "<<file_name<<std::endl;
exit(-1);
}
std::map<int, WORD*> map;
std::vector<std::string> tokens;
std::string line;
char word[100];
int size;
while (std::getline(infile, line)) {
size = (int)split(line, tokens, ' ');
WORD* entry = (WORD*) malloc(sizeof(WORD*));
entry->id = atoi(tokens[0].c_str());
entry->count = atoi(tokens[2].c_str());
strcpy(word, tokens[1].c_str());
entry->word = word;
map[entry->id] = entry;
std::cout<< entry->id<<" "<<entry->word<<" "<<entry->count<<std::endl;
}
infile.close();
std::cout<<map.size()<<std::endl;
std::map<int, WORD*>::const_iterator it;
for (it = map.begin(); it != map.end(); it++) {
std::cout<<(it->first)<<" "<<(it->second->word)<<std::endl;
}
return map;
}
//split string by a delimiter
size_t split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
size_t pos = txt.find( ch );
size_t initialPos = 0;
strs.clear();
while( pos != std::string::npos ) {
strs.push_back( txt.substr( initialPos, pos - initialPos + 1 ) );
initialPos = pos + 1;
pos = txt.find( ch, initialPos );
}
strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
return strs.size();
}
数据文件:
2 I 1
3 gave 1
4 him 1
5 the 3
6 book 3
7 . 3
8 He 2
9 read 1
10 loved 1
结果:
2 I 1
3 gave 1
4 him 1
5 the 3
6 book 3
7 . 3
8 He 2
9 read 1
10 loved 1
map size:9
2 loved
3 loved
4 loved
5 loved
6 loved
7 loved
8 loved
9 loved
10 loved
WORD* entry = (WORD*) malloc(sizeof(WORD*));
分配一个CCD_ 1而不是整个CCD_。
编译器不断地分配条目,如果没有初始化为任何东西(它们都指向某个随机地址,该地址甚至可能不属于您的程序。(,然后您重复地将该指针添加到映射中。因此,地图上的所有第一个都指向同一位置(巧合地(。应该是
WORD* entry = new WORD;
这是一种更清洁的方式
struct WORD{
int id;
int count;
std::string word;
};
while (std::getline(infile, line)) {
WORD* entry = new WORD;
std::istringstream iss(line);
iss >> entry->id >> entry->word >> entry->count;
map[entry->id] = entry;
std::cout<< entry->id<<" "<<entry->word<<" "<<entry->count<<std::endl;
}
您忘记在strcpy
之前为WORD::word
分配内存。您将char word[100]
的地址分配给映射的所有项,这对所有项都是相同的。
最好使用std::string
而不是C样式的字符串。此外,您可以使用std::stoi
将字符串转换为整数。试试这个:
struct WORD{
int id;
int count;
std::string word;
};
std::map<int, WORD> readWordMap(const std::string &file_name)
{
...
std::map<int, WORD> map;
...
while (std::getline(infile, line)) {
...
WORD entry;
entry.id = std::stoi(tokens[0]);
entry.count = std::stoi(tokens[2]);
entry.word = tokens[1];
map[entry.id] = entry;
...
}
infile.close();
...
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 在c++中访问int到类对象的映射时出错
- 在C++中搜索嵌套多映射值
- 错误处理.将系统错误代码映射到泛型
- C++匿名结构作为std::映射值
- 如何从存储在std::映射中的std::集中删除元素
- 递归无序映射
- 正在VS调试器中监视映射条目