简体   繁体   English

hash_map在c ++ stl中崩溃

[英]hash_map crashing in c++ stl

i am relatively experienced in Java coding but am new to C++. 我在Java编码方面比较有经验,但我是C ++的新手。 I have written the following C++ code as solution to the USACO training problem which I have reproduced at this url 我已经编写了以下C ++代码作为USACO培训问题的解决方案,我已在此网址上复制了问题

This code looks fine to me. 这段代码对我来说很好看。 However it crashes on the sample test case given. 然而,它在给出的样本测试用例中崩溃了。 On isolating the error, I found that if the second for loop is not run for the last iteration (I mean like in the sample test case, n = 5, so I run the loop only till i = 3 instead of i = 4), then it doesn't crash (and produces the expected output). 在隔离错误时,我发现如果第二次for循环没有运行到最后一次迭代(我的意思是在示例测试用例中,n = 5,所以我只运行循环直到i = 3而不是i = 4) ,然后它不会崩溃(并产生预期的输出)。 Maybe the error is somewhere else, I can't detect it. 也许错误是在其他地方,我无法检测到它。

Any ideas are welcome. 欢迎任何想法。 Thanks in advance. 提前致谢。 Please excuse me for the slightly unwieldy formatting of the code (this is my first forum post). 请原谅我稍微笨拙的代码格式(这是我的第一篇论坛帖子)。 The files included are stdlib.h, stdio.h and hash_map.h ` 包含的文件是stdlib.h,stdio.h和hash_map.h`

#include <stdlib.h>
#include <stdio.h>
#include <hash_map.h>

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};


int main(int argc, char** argv) {

    FILE *fin = fopen("gift1.in", "r");
    FILE *fout = fopen("gift1.out", "w");


    hash_map<const char*, int, hash<const char*>, eqstr> table;

    int n;
    fscanf(fin,"%d",&n);
    char name[15];
    char people[10][15];

    for(int i = 0; i < n; i++){
        fscanf(fin,"%s",name);
        strcpy(people[i],name);
        table[people[i]] = 0;
    }//ifor

    for(int i = 0; i < n; i++){
        fscanf(fin,"%s",name);
        int money;
        fscanf(fin,"%d",&money);
        int friends;
        fscanf(fin,"%d",&friends);
        char fname[15];
        int amt = money/friends;
        for(int j = 0; j < friends; j++){
            fscanf(fin,"%s",fname);
            table[fname] = table[fname] + amt;
        }//jfor
        table[name] = table[name] - friends*amt;
    }//ifor

    for(int i = 0; i < n; i++)
        fprintf(fout,"%s %d\n",people[i],table[people[i]]);

    return (EXIT_SUCCESS);
}

` `

The reason it is crashing is that vick is giving 0 friends money which causes a divide by zero exception from the following line of code: int amt = money/friends; 它崩溃的原因是vick给了0个朋友的钱,导致从以下代码行中除以零例外: int amt = money/friends;

You should put in some special logic to handle the case when the person has 0 friends so gives $0 away. 当这个人有0个朋友时,你应该加入一些特殊的逻辑来处理这个案例。

As was stated in the other comments, you should use some stl classes (string,iostream, etc) to help clean up the code. 正如其他注释中所述,您应该使用一些stl类(字符串,iostream等)来帮助清理代码。

Edit: Added the input data so the question and answer would make a little more sense 编辑:添加了输入数据,因此问题和答案会更有意义

5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0

I would suggest using GDB to find these errors. 我建议使用GDB来查找这些错误。 These occured to me also. 这些也发生在我身上。

Compile with -g flag, then use gdb a.out(executable). 使用-g标志编译,然后使用gdb a.out(可执行文件)。 Now type run to run the program. 现在输入run来运行程序。 Once the program crashes, you can use backtrace to identify the exact line where its crashing and the variable values at that point. 程序崩溃后,您可以使用backtrace来识别崩溃的确切行和该点的变量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM