【问题标题】:How to convert an integer to an array of its digits in C++如何在 C++ 中将整数转换为其数字的数组
【发布时间】:2020-02-21 22:47:26
【问题描述】:

假设我有整数 1004。 我想使用以下模式将其存储在数组 A 中:

    A[0]=1
    A[1]=0
    A[2]=0
    A[3]=4

如何获得该索引处的值? 如何在 C++ 中做到这一点?

【问题讨论】:

  • SO 是一个帮助您解决特定问题的网站。站点指南实际上要求您首先尝试解决问题,并说明您失败的方式和原因。请阅读如何构建minimal reproducible example
  • '那个索引'什么索引?
  • 你的意思是把一个数字分成它的位数?那么this question 可能会有所帮助。或者您只需将其转换为字符串。
  • 这能回答你的问题吗? How do I split an int into its digits?

标签: c++ arrays


【解决方案1】:

您可以使用模 10 获得数字的最后一个索引,然后通过将该数字除以 10 来删除该值。

所以假设你这样做:

1004 % 10 = 4
1004 / 10 = 100

然后对每个数字重复此操作

【讨论】:

  • 注意,这不会存储 OP 请求的值
  • 这只是将获得的值以相反的顺序放入数组中的问题,但我认为 OP 的主要问题是获取它们。如果他真的不知道该怎么做,我可能会更新答案
【解决方案2】:

使用静态内存:

int originalNumber = 1004;
int digitArray[10] = {0};
int idx = 0;

while (originalNumber > 0)
{
    int digit = n % 10;
    originalNumber /= 10;
    digitArray[idx] = digit;
    ++idx;
}

// Reverse the order of the array
std::reverse(std::begin(digitArray), std::begin(digitArray)+(idx-1));

【讨论】:

    【解决方案3】:

    我不确定这是否是最有效的方法,但它确实有效。

    您可以将数字中的每个数字输入到数组中,但从数组的末尾到开头。

    例如,有一个大小为 4 的数组,所以你得到数字的最后一位是这样的:num % 10,并将该位压入数组的第三个索引。

    代码示例:

    #define SIZE 4
    
    int* numToArray(int num)
    {
        int* arr = new int[SIZE]; // assuming you already know the number of digits in the number
        for(int i = SIZE-1; i >= 0; i++)
        {
            arr[i] = num % 10; // Enters the last digit to the array
            num /= 10; // Gets rid of the last digit in the number
        }
        return arr;
    }
    

    【讨论】:

      【解决方案4】:

      建议你用std::vector代替普通的整数数组。

      然后,你可以有如下的东西:

      #include <iostream>
      #include <vector>
      
      int main() {
          int number = 1004;
          std::vector<int> digits;
      
          while (number != 0) {
              digits.insert(digits.begin(), number % 10);
              number /= 10;
          }
      
          for (auto const i : digits) {
              std::cout << i << " "; // 1 0 0 4
          }
      
          // or by index
      
          std::cout << std::endl;
      
          std::cout << "[0]" << digits[0] << std::endl; // 1
          std::cout << "[1]" << digits[1] << std::endl; // 0
          std::cout << "[2]" << digits[2] << std::endl; // 0
          std::cout << "[3]" << digits[3] << std::endl; // 4
      
          return 0;
      }
      

      Demo

      【讨论】:

        【解决方案5】:

        除了所有现有的答案之外,我想提出一种更优雅,但可能效率更低的方法,利用标准库的许多奇迹。

        #include <string>
        #include <vector>
        #include <algorithm>
        #include <iostream>
        
        int main()
        {
            auto number = 1004;
            auto asString = std::to_string(number); // 
            std::vector<int> digits(asString.length());
            std::transform(asString.begin(), asString.end(), digits.begin(), [](char c){return c -'0';});
        
            for(auto d : digits)
            {
                std::cout << d << ' ';
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-03-19
          • 1970-01-01
          • 1970-01-01
          • 2016-04-15
          • 2021-10-21
          • 1970-01-01
          • 1970-01-01
          • 2011-08-13
          • 2018-10-07
          相关资源
          最近更新 更多