如何在不使用字符串的情况下将两个数字的数字存储在下面代码中的数组中?

How can I store the digits of two numbers in an array like in the code below, without using a string?

我需要一个程序来读取两个数字并将这些数字的数字存储在带有“;”的数组中在他们之间。我尝试使用 char 数组,但它似乎对我不起作用,而且我也尝试过,如下所示,首先将数字存储在字符串中,然后放置一个 ';'在两者之间然后将它们存储在数组中。没有字符串怎么办?

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int a,b;
    char v[99999];
    string numTotal;
    cin>>a>>b;
    numTotal=to_string(a)+';'+to_string(b);
    for(int i=0;i<numTotal.length();i++){
        v[i]=numTotal[i];
        cout<<v[i];
    }
}

您可能想使用一个名为 getline(std::cin,) 的函数(只要您不按特定关键字,如:Enter 或 sth)它会一次性获取所有字符串(您可以写 3;4 或 sth 并且它会存储它 word-by-word).

getline(cin,numTotal);

字符串是最简单的方法,但是如果你不想使用字符串,你可以像这样使用std::stack

int main() {
    int a, b, n;
    cin >> a >> b;
    stack<int> aStack;
    n = a;
    while(n > 0) {
        aStack.push(n % 10);
        n /= 10;
    }
    stack<int> bStack;
    n = b;
    while(n > 0) {
        bStack.push(n % 10);
        n /= 10;
    }
    while(!aStack.empty()) {
        cout << aStack.top() << ' ';
        aStack.pop();
    }
    cout << '\n';
    while(!bStack.empty()) {
        cout << bStack.top() << ' ';
        bStack.pop();
    }
}