繁体   English   中英

将2D数组传递给函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM