Вы можете сделать это, если замените прототип:
Node* construct(int arr[][3], int i, int j,
int m, int n)
{
//...
}
Автор:
Node* construct(const vector<vector<int>> & arr, int i, int j,
int m, int n)
{
//...
}
Таким образом, это должно работать, так как вы можете получить доступ к элементам векторов с помощью operator[]
.
Я надеюсь, что это может решить вашу проблему.
EDIT:
Чтобы избежать предупреждений, вы могли бы даже написать:
Node* construct(const vector<vector<int>> & arr, size_t i, size_t j,
size_t m, size_t n)
{
//...
}
EDIT2: полный пример кода
Я использовал именно тот код, который вы дали нам в своем вопросе:
// CPP program to construct a linked list
// from given 2D matrix
#include <bits/stdc++.h>
using namespace std;
// struct node of linked list
struct Node {
int data;
Node* right, *down;
};
// returns head pointer of linked list
// constructed from 2D matrix
Node* construct(const vector<vector<int>> & arr, size_t i, size_t j,
size_t m, size_t n)
{
// return if i or j is out of bounds
if (i > n - 1 || j > m - 1)
return nullptr;
// create a new node for current i and j
// and recursively allocate its down and
// right pointers
Node* temp = new Node();
temp->data = arr[i][j];
temp->right = construct(arr, i, j + 1, m, n);
temp->down = construct(arr, i + 1, j, m, n);
return temp;
}
// utility function for displaying
// linked list data
void display(Node* head)
{
// pointer to move right
Node* Rp;
// pointer to move down
Node* Dp = head;
// loop till node->down is not NULL
while (Dp) {
Rp = Dp;
// loop till node->right is not NULL
while (Rp) {
cout << Rp->data << " ";
Rp = Rp->right;
}
cout << "\n";
Dp = Dp->down;
}
}
// driver program
int main()
{
// 2D matrix
vector<vector<int>> arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
size_t m = 3, n = 3;
Node* head = construct(arr, 0, 0, m, n);
display(head);
return 0;
}
Я заменил ваш код на более эффективную и удобочитаемую векторную инициализацию.
Надеюсь, это поможет вам:)