std :: map不符合预期 [英] std::map not behaving as expected

查看:230
本文介绍了std :: map不符合预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 std :: map 模板,但我无法使其正常工作。从互联网研究我来到这个解决方案,并将一个文件重定向到输入流,这里是代码:

I am trying to use the std::map template, but I haven't been able to get it working. From research on the internet I've come to this solution and redirecting the a file to the input stream, here is the code:

typedef map<char*, int> wc;
int main() {
    int c;
    char cc[75], nombre[75];
    wc m;
    scanf("%d", &c);
    while (c--) {
        scanf("%s %[ a-zA-Z]", cc, nombre);
        ++m[cc]; // This should work
        printmap(m);
    }
}

打印地图是一个只打印地图对象的功能。这是我的文件input.txt

Print map is a function that just prints the map object. Here is my file input.txt

3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna

当我执行程序时,输出是:

When I execute the program, the ouput is:

Spain -> 1
England -> 2
Spain -> 1

我期待的是:

Spain -> 2
England -> 1

映射到出现次数的国家/地区的出现次数

The number of occurrences of the Country mapped to the number of times it appears

推荐答案

作为上述我的评论的解决方案,您提供的代码的C ++版本:

As a solution to my comment above, the C++ version of the code you presented:

typedef map<std::string, int> wc;
int main() {
    int c;
    string cc, nombre;
    wc m;
    std::cin >> c;
    while (c--) {
        std::cin >> cc;
        std::getline(std::cin, nombre);
        m[cc] += 1; // This should work
    }
    printmap(m);
}

http://ideone.com/2JP82

首先: std :: map 根据您的代码 char * 中的密钥对其进行排序,其中指向 char cc [75] 。因此,当您更换 cc 中的文本时,地图的键会更改,并且会破坏所有内容。地图密钥永远不能更改。由于我们使用C ++,所以你不应该使用 char [] 使用 std :: string ,而(因为它是一个值类型)将使所有的东西都能神奇地工作。我不知道它是如何工作的,因为你没有显示 printmap 函数。

First: std::map sorts it's data based on the key, in your code, char*, which points at char cc[75]. So when you replaced the text in cc, then the keys of the map changed, and that breaks everything. The keys of a map must not change ever. Since we're using C++, you should not use char[] at all; use std::string instead, which (since it is a "value type") will make everything just magically work. I have no idea how it was working before, since you don't show the printmap function.

第二:您每次阅读单行时都会调用 printmap ,而且由于映射无法打印最后添加的东西,这个想法根本没有意义。 printmap 调用应该可能打印整个地图,并且在循环之外。

Second: You call printmap each and every time you read a single line, and since a map has no way of printing "the last thing added", that idea makes no sense at all. The printmap call should probably print the entire map, and be outside the loop.

第三:不要使用 scanf ,这是不安全的。使用流: std :: cin>> cc 用于阅读单个单词,或 std :: getline(std :: cin,nombre),用于阅读剩余的内容。这样一来,如果有人进入该行,代码不会崩溃(最长国家/地区名称的来源和最长的姓氏

Third: Don't use scanf, it's not safe. Use streams: std::cin >> cc for reading in a single word, or std::getline(std::cin, nombre) for reading in what's left on the line. That way the code won't crash if someone enters the line (sources for longest country name and longest last name)

Krungthepmahanakornamornratanakosinmahintarayuttha-yamahadilokphopnopparatrajathaniburiromudomrajaniw-esmahasatharnamornphimarnavatarnsathitsakkattiyavi-sanukamprasit Wolfeschlegelsteinhausenbergerdorffvortschaferswesenchafewarenwholgepflegeunzenvonangereifenduchihrraubgiriigfeindtausendjahresvorandieerscheinenbanderechiffgebrauchlichtalsseinursprungvonkrrthinzwischensternartigraumaufdersuchebtbewohnbarplanetenkreisedrehensichundstandigmenshlichkeittkonntevortpflanzeslamdlichfreudeundruhemitnichteinfurchrintlligentgeschopfsvonhinzwischenster

这篇关于std :: map不符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆