如何在 C++ 中将 int 数组放入 int 二维矩阵?

How to put int arrays into int 2d matrix in C++?

当我尝试将 int 数组放入 int 矩阵时,我写了这个(内存错误):

#include <iostream>

using std::cout; using std::endl;

int* intToIntArray(int input, int length) {
    int output[20];
    for (int i = 0; i < length; i++) {
        output[i] = input % 10;
        input /= 10;
        cout << output[i];
    }
    cout << endl;
    cout << "=====" << endl;
    return output;
}

int main() {
    const int arraySize = 5;
    int a[arraySize] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[arraySize] = { 6,7,8,9,10 };
    int* arr1D;
    int** arr2D = new int*;

    for (int counter = 0; counter < arraySize; counter++) {
        cout << a[counter] << endl;
        arr1D = intToIntArray(a[counter], NumSize[counter]);
        arr2D[counter] = arr1D;
    }


    for (int i = 0; i < arraySize; i++) {
        for (int j = 0; j < NumSize[i]; j++) {
            cout << arr2D[i][j];
        }
        cout << endl;
    }
}

我知道如何将数据从 int 数组放入 int 矩阵,但效率非常低:

#include <iostream>

using std::cout; using std::endl;

int* intToIntArray(int input, int length) {
    int output[20];
    for (int i = length-1; i >= 0; i--) {
        output[i] = input % 10;
        input /= 10;
        cout << output[i];
    }
    cout << endl;
    cout << "=====" << endl;
    return output;
}

int main() {
    const int arraySize = 5;
    int a[arraySize] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[arraySize] = { 6,7,8,9,10 };
    
    int arr2D[20][20];
    
    for (int i = 0; i < arraySize; i++) {
        for (int j = 0; j < NumSize[i]; j++) {
            arr2D[i][j] = intToIntArray(a[i], NumSize[i])[j];
        }
    }

    for (int i = 0; i < arraySize; i++) {
        for (int j = 0; j < NumSize[i]; j++) {
            cout << arr2D[i][j];
        }
        cout << endl;
    }
}

我如何有效地做到这一点? 我不想使用任何向量,因为我想稍后将它编码到我的 GPU 中。 有没有一种有效的方法可以将 int 数组直接插入到 int 矩阵中?

编辑:(我做了我想做的事,我只是把它设为静态...)

#include <iostream>

using std::cout; using std::endl;

int* intToIntArray(int input, int length) {
    static int output[20];
    for (int i = 0; i < length; i++) {
        output[i] = input % 10;
        input /= 10;
    }
    return output;
}

int main() {
    const int arraySize = 5;
    int a[arraySize] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[arraySize] = { 6,7,8,9,10 };
    int arr2D[20][20];

    for (int counter = 0; counter < arraySize; counter++) {
        int *arr1D = intToIntArray(a[counter], NumSize[counter]);

        for (int i = 0; i < NumSize[counter]; i++) {
            arr2D[counter][i] = arr1D[NumSize[counter]-1-i];
        }
    }

    for (int i = 0; i < arraySize; i++) {
        for (int j = 0; j < NumSize[i]; j++) {
            cout << arr2D[i][j];
        }
        cout << endl;
    }
}
#include <iostream>
using std::cout; using std::cin;
void intToIntArray(int input, int length, int* &output) {
    output = new int[length];
    for(int i = length - 1; i >= 0; --i) {
        output[i] = input % 10;
        input /= 10;
    }
}
int main() {
    const int arraySize = 5;
    int a[] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[] = { 6,7,8,9,10 };
    int** arr2D = new int*[arraySize];
    for(int i = 0; i < arraySize; ++i) {
        intToIntArray(a[i], NumSize[i], arr2D[i]);
    }
}