stoi() 在抛出 c++ 中的 'std::invalid argument in c++ 实例后终止 - 我做错了什么?

发布于 2025-01-10 17:28:00 字数 722 浏览 0 评论 0原文

对编码相当陌生。在 LeetCode 上尝试一些简单的项目,但失败了……哈!我正在尝试获取一个整数并将其转换为字符串,以便我可以反转它,然后将反转的字符串重新转换回整数。

此代码抛出“抛出并实例'std::invalid argument'what(): stoi”错误。我花了一个小时在谷歌上搜索谷歌和其他问题,但不明白为什么它不起作用。

bool isPalindrome(int x) {
  std::string backwards ="";
  std::string NumString = std::to_string(x);
     
  for (int i = NumString.size(); i >= 0 ; i--) {
    backwards += NumString[i];
  }
       
  int check = std::stoi(backwards);
              
  if (check == x) {
    return true;
  }
  else {
    return false;
  }
}

编辑:我认为我已经弄清楚了。它在第一次转换时将空字符添加到字符串的末尾,然后在反转它时将其添加到字符串的开头。空格不能转换为整数。

所以...我改变了这一行并且它有效:

for (int i = NumString.size() - 1; i >= 0 ; i--) 

Fairly new to coding. Trying some of the easy projects at LeetCode, and failing... Ha! I am trying to take an integer and convert it to a string so I can reverse it, then re-convert the reversed string back into a integer.

This code is throwing the "terminate after throwing and instance of 'std::invalid argument' what(): stoi" error. I've spent an hour searching google and other questions here on SO, but can't figure out why it's not working.

bool isPalindrome(int x) {
  std::string backwards ="";
  std::string NumString = std::to_string(x);
     
  for (int i = NumString.size(); i >= 0 ; i--) {
    backwards += NumString[i];
  }
       
  int check = std::stoi(backwards);
              
  if (check == x) {
    return true;
  }
  else {
    return false;
  }
}

EDIT: I think I figured it out. It was adding the null character to the end of the string upon first conversion, then adding it to the beginning of the string when I reversed it. Spaces can't be converted to integers.

So... I changed this line and it works:

for (int i = NumString.size() - 1; i >= 0 ; i--) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2025-01-17 17:28:00

您还可以在不使用字符串的情况下反转数字。

     bool isPalindrome(int x) {
        long long  rev = 0;
        int cur = x;
        while( cur > 0) {
            rev *= 10;
            rev += cur % 10;
            cur /=10;
        }
        return rev == x;

    }

you can also reverse number without using string.

     bool isPalindrome(int x) {
        long long  rev = 0;
        int cur = x;
        while( cur > 0) {
            rev *= 10;
            rev += cur % 10;
            cur /=10;
        }
        return rev == x;

    }
梦太阳 2025-01-17 17:28:00

它比你编辑的答案更简单。你

  for (int i = NumString.size(); i >= 0 ; i--) {
    backwards += NumString[i];
  }
 

想象一下 Numstring 的长度为 3 (无论空格、数字......)

所以现在你正在有效地做

  for (int i = 3; i >= 0 ; i--) {
    backwards += NumString[i];
  }
 

所以第一个循环

  backwards += NumString[3];

顺利地在数组中的事物的索引C++中的长度3是0,1,2。你即将结束

这就是为什么你会看到循环

 for(int i = 0; i < len; i++){}

注意 i i i i i i i i < len 不是 i <= len

Its simpler than your answer that you edited in. YOu have

  for (int i = NumString.size(); i >= 0 ; i--) {
    backwards += NumString[i];
  }
 

Imagine that Numstring has length 3 (no matter what spaces, digits,....)

So now you are efectively doing

  for (int i = 3; i >= 0 ; i--) {
    backwards += NumString[i];
  }
 

So first loop goes

  backwards += NumString[3];

well the indexes of things in an array of length 3 in c++ are 0,1,2. YOu are going one off the end

This is why you see loops doing

 for(int i = 0; i < len; i++){}

Note the i < len not i <= len

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文