stoi"在抛出' std :: invalid argument'的实例后终止调用

时间:2015-11-21 09:21:38

标签: c++

我目前正在研究动态编程问题,我决定使用C ++编写代码而不是通常的Java / C#来提高我的技能。 但是,我收到一个我无法弄清楚的错误。编辑:我将在此发布我的整个解决方案:

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int linecounter = 0;
    int value = -1;
    int differentCoins = -1;
    int uniqueCoinCount = -1;
    std::vector<std::vector<int>> array;
    int *uniqueCoins;
    int totalAmount = -1;

    for (std::string line; std::getline(std::cin, line); ) {
        if (linecounter % 2 == 0) {
            string inputs[2];
            int i = 0;
            stringstream ssin(line);
            while (ssin.good() && i < 2){
                ssin >> inputs[i];
                ++i;
            }
            cout << inputs[0];
            cout << inputs[1];
            totalAmount = std::stoi(inputs[0]);
            uniqueCoinCount = std::stoi(inputs[1]);
            uniqueCoins = new int[uniqueCoinCount + 1];

        }
        if (linecounter % 2 == 1) {
            array.resize(uniqueCoinCount + 1, std::vector<int>(totalAmount + 1));
            for (int i = 0; i < totalAmount; i++) {
                array[i][0] = 0;
            }
            for (int i = 0; i <= uniqueCoinCount; i++) {
                array[0][i] = 1;
            }


            stringstream ssin(line);
            int coinCounter = 0;
            uniqueCoins[coinCounter] = 0;
            coinCounter++;
            while (ssin.good() && coinCounter < uniqueCoinCount + 1){

                ssin >> uniqueCoins[coinCounter];
                coinCounter++;
            }

            for (int i = 1; i < totalAmount; i++) {
                for (int j = 1; j <= uniqueCoinCount; j++) {
                    int totalExcludingCoin = array[i][j - 1];
                    if (uniqueCoins[j] <= i) {
                        array[i][j] = totalExcludingCoin + array[i - uniqueCoins[j]][j];
                    }
                    else {
                        array[i][j] = totalExcludingCoin;
                    }
                }
            }
        }
        linecounter++;
    }

    //cout << array[totalAmount][uniqueCoinCount + 1];
}

当我使用cout << inputs[0]时,我可以看到它打印出4。但是,hackerrank编译器给我一个错误说

  

&#34;在抛出&#39; std::invalid_argument&#39;
的实例后终止   what(): stoi&#34;

可能是什么问题?我还尝试了atoi(),它返回40而不是4,可能是因为atoi在错误时返回0?是因为它读取空终止符还是什么?

由于

以下是hackerrank的输出:

Nice try, but you did not pass this test case.

Input (stdin)
4 3
1 2 3 

Your Output (stdout)
43

Expected Output
4

Compiler Message
Abort Called
Error (stderr)
terminate called after throwing an instance of 'std::invalid_argument'  
what():  stoi

2 个答案:

答案 0 :(得分:2)

        while (ssin.good() && i < 2){
            ssin >> inputs[i];
            ++i;
        }

您正在使用状态报告功能来预测未来。 good函数不会告诉您将来操作的结果,而只会告诉您过去操作的结果。您实际上需要检查操作(ssin >> inputs[i]是否成功,而不是假设过去的流很好就必须成功。

答案 1 :(得分:0)

在竞争性编程中,出于速度原因习惯使用scanf:http://www.cplusplus.com/reference/cstdio/scanf/

另一个好方法是使用cin。我相信它有点慢,但我已经接受它了。

为大多数比赛从stdin读取的示例代码。

int main() {
  int n;
  cin >> n;

  std::vector<int> xs(n);
  for (int i = 0; i < n; ++i) { 
    cin >> xs[i];
  }
  // do actual computation
}