无法找到我存储在地图中的值

雷·雷

我基本上是逐个字符地读取文件,当遇到一个新数字时,我会将其存储为astd::map作为元素等于1的键。越是遇到相同数字的次数越多,我将该键的元素递增:

if(intMap.count(singleCharacter)){
    //keyexist  just increment count else
    //cout<<"exist"<<endl;
                
    int newValue =  intMap.at(singleCharacter) + 1; //grabs the value that already exists and we need to incrmenet it by 1 and store the new value in the map.

    std::map<int, int>::iterator it = intMap.find(singleCharacter); //finds the single character and increments it by 1.

    if (it != intMap.end())
        it->second = newValue; //setting the new increment value into the element
}else{
    //doesnt exist and and we need to create it and incrmenet by 1
    //cout<<"doesnt exist"<<endl;
    intMap.insert(pair<int, int>(singleCharacter,1));
    cout<<singleCharacter <<" new made : "<<intMap.at(singleCharacter) <<endl;
    }
}

for (auto& p : intMap ) {
    cout << p.first<<": "<< p.second <<endl;; // "Karl", "George"
}

唯一的问题是,当我尝试打印出地图中的所有值时,它会为我提供随机数,而我不知道它们的来源。

这是我正在读取的文件:

An International Standard Book Number (ISBN) is a code of 10 characters, referred to as ISBN-10,                                                                             
 separated by dashes such as 0-7637-0798-8. An ISBN-10 consists of four parts: a group code, a publisher code,                                                                
 a code that uniquely identifies the book among those published by a particular publisher, and a check character.                                                             
 The check character is used to validate an ISBN. For the ISBN 0-7637-0798-8, the group code is 0,                                                                            
 which identifies the book as one from an English-speaking country. The publisher code 7637 is for "Jones and Bartlett Publishers

我得到的输出:

48: 8                                                                                                                                                                          
49: 3                                                                                                                                                                          
51: 3                                                                                                                                                                          
54: 3                                                                                                                                                                          
55: 8                                                                                                                                                                          
56: 4                                                                                                                                                                          
57: 2  

我应该得到的输出应该是这样的:

1: and the amount of times it was seen 

任何数字都一样。

斯拉瓦

之所以得到这些“随机”数字,是因为您使用std::map<int,int>而不是std::map<char,int>您得到的是字符符号的ASCII数字代码及其计数。

因此,您需要更改地图的类型,或将键投射回char

for (auto& p : intMap ) {
    cout << static_cast<char>(p.first) << ": "<< p.second <<endl;
}

注意:std::map::operator[]正是针对这种情况而设计的,在这种情况下,它将使用0初始化缺失值,因此您的所有条件都可以替换为:

intMap[singleCharacter]++;

可以在本文档中找到详细信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章