将整数向量转换为 const int* const* 类型的二维数组

Convert Vector of integers into 2d array of type const int* const*

我有一个整数向量,我应该把它变成一个二维数组。新 2d 数组的行和列大小由用户指定,并包含来自先前向量的所有整数。二维数组应该是 const int* const* 类型。我如何在 C++ 中执行此操作?

尝试这样的事情:

std::vector<int> nums;
// fill nums as needed...

int rows, cols;
std::cin >> rows >> cols;

if ((rows * cols) > nums.size())
    // error

int** arr = new int*[rows];
for (int row = 0; row < rows; ++row) {
    arr[row] = new int[cols];
    for (int col = 0; col < cols; ++col) {
        arr[row][col] = nums[(row*cols)+col];
    }
}

// use arr as needed...

for (int row = 0; row < rows; ++row) {
    delete[] arr[row];
}
delete[] arr;