【问题标题】:Getting strange numerical values when trying to store digits of a string in an array尝试将字符串的数字存储在数组中时得到奇怪的数值
【发布时间】:2021-09-28 08:03:01
【问题描述】:

程序非常简单。该程序的基本目的是将一个数字作为用户的输入,然后将该数字的各个数字存储在一个数组中。为此,我的方法是首先将输入整数转换为字符串。然后我们将遍历字符串的每个字符并将字符转换为数字并将其存储在数组中。 首先我输入数字的长度。然后我输入数字。然后我将该整数转换为字符串,然后遍历字符串的每个位置并将后续数字存储在数组中。 代码:

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int m;
    cin>>m;
    string s = to_string(m);
    int arr[n];
    int j=0;
    for(auto d:s){
        arr[j] = d-'0';
        j+=1;
    }
    for(int i=0;i<n;i++) cout<<arr[i];
}

该代码对所有不以“0”开头的数字都非常有效。但是一旦我给出一个像'0135'这样的输入整数,我就会得到像'13532764'这样的奇怪数组元素。 请帮我找出问题。

【问题讨论】:

  • int arr[n]; 不是标准 C++,请改用 std::vector。在任何情况下,在使用它输出的int 值之前,您都不会检查operator&gt;&gt; 是否成功。在m 之前阅读n 有什么意义?你有m在一个string,只需使用字符串的size()来分配int数组,不需要n
  • 问问自己:在int arr[n];中,n的值是多少?
  • 注意:使用调试器可以很容易地发现问题。即使您不知道如何使用它,散布在各处的一些打印语句也会有所帮助。例如:cout &lt;&lt; n &lt;&lt; " " &lt;&lt; m &lt;&lt; " " &lt;&lt; s &lt;&lt; "\n"; 将显示 s 只有 3 个字符。
  • 即使在arr[] 的数字之间放置一个空格也有助于发现问题,例如:for(int i = 0; i &lt; n; i++) cout &lt;&lt; arr[i] &lt;&lt; ' '; 然后你会看到输出为1 3 5 32764

标签: c++ arrays string


【解决方案1】:

当您将0135 之类的东西作为m4 作为n 时,arr[n] 将包含 4 个元素。但是您来自 std::to_string(135) 的字符串仅包含 3 个字符。因此,arr 的最后一个元素不会被初始化,它可以有任何随机值,因此您可能会在所需数字后面看到随机数字。

【讨论】:

    【解决方案2】:

    前导零对整数没有任何意义,因此当您从用户输入中将0135 作为int 读取时,它将输出int135。因此,在将135 转换为std::string 之后,您最终只会将 3 位数字放入 4 位数组中,而第 4 位数字未分配。由于您没有初始化数组,因此第 4 位数字具有随机值,在您的情况下为 32764 (0x7FFC)。 Demo

    你根本不需要n。在std::string 中添加m 后,您可以改用字符串的size(),例如:

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main(){
        int m;
        cin >> m;
        string s = to_string(m);
        vector<int> arr(s.size());
        int j = 0;
        for(auto ch : s){
            arr[j] = ch - '0';
            ++j;
        }
        for(auto d : arr)
            cout << d;
    }
    

    Demo

    如果您想保留前导零,则不要将输入读入int,而是将其读入std::string,然后根据需要验证字符,例如:

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main(){
        string s;
        cin >> s;
        vector<int> arr(s.size());
        int j = 0;
        for(auto ch : s){
            if (ch >= '0' && ch <= '9'){
                arr[j] = d - '0';
                ++j;
            } else {
                ...
            }
        }
        for(int i = 0; i < j; ++i)
            cout << arr[i];
    }
    

    Demo

    【讨论】:

      【解决方案3】:

      这个循环后确定数字的长度

      for(auto d:s){
          arr[j] = d-'0';
          j+=1;
      }
      

      并且等于变量j的值。

      所以至少在下面的循环中,你应该使用变量 j 而不是 n

      for ( int i=0; i < j; i++) cout<<arr[i]; 
                     ^^^^^^
      

      但在任何情况下,使用您的方法都容易出错,因为可变长度数组 arr(这不是标准 C++ 功能)的大小可能小于输入数字中的位数。此外,用户可以输入负数。:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多