[英]Pass a 2d array to a function
我經常處理圖形,發現很難將鄰接表傳遞給函數。
void myfunc(int ad[][])//proper way to receive it?
{
}
int main()
{
int N;//number of vertices
cin >> N;
int ad[N][N];
for(int i = 0; i<N; i++)
for(int j = 0;j<N;j++)
cin >> ad[i][j];
myfunc(ad); //proper way to pass it?
return 0;
}
抱歉,如果這是一個菜鳥問題,但我發現人們傳遞的二維數組只有固定的和已知的尺寸。 不知道該怎么做。
可變長度數組(VLA)在C ++中不是標准的。
使用std::vector
代替:
void myfunc(const std::vector<std::vector<int>>& ad)
{
}
int main()
{
int N;//number of vertices
cin >> N;
std::vector<std::vector<int>> ad(N, std::vector<int>(N));
for(int i = 0; i<N; i++)
for(int j = 0;j<N;j++)
cin >> ad[i][j];
myfunc(ad);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.