我有一个函数,我想把它作为一个参数,一个可变大小的二维数组。

到目前为止,我有这个:

void myFunction(double** myArray){
     myArray[x][y] = 5;
     etc...
}

我在代码的其他地方声明了一个数组:

double anArray[10][10];

然而,调用myFunction(anArray)会给我一个错误。

我不想在传入数组时复制它。在myFunction中所做的任何更改都应该改变anArray的状态。如果我理解正确,我只想传递一个指向2D数组的指针作为参数。该函数还需要接受不同大小的数组。比如[10][10][5][5]。我该怎么做呢?


当前回答

将二维数组传递给函数有三种方法:

参数为2D数组 int数组[10][10]; void passFunc(int a[][10]) { / /…… } passFunc(数组); 形参是一个包含指针的数组 int数组* [10]; For (int I = 0;I < 10;我+ +) 数组[i] = new int[10]; void passFunc(int *a[10]) //指针数组 { / /…… } passFunc(数组); 形参是指向指针的指针 int * *数组; 数组= new int *[10]; For (int I = 0;我< 10;我+ +) 数组[i] = new int[10]; void passFunc(int **a) { / /…… } passFunc(数组);

其他回答

你可以像这样创建一个函数模板:

template<int R, int C>
void myFunction(double (&myArray)[R][C])
{
    myArray[x][y] = 5;
    etc...
}

然后你通过R和c有两个维度大小,每个数组大小都会创建一个不同的函数,所以如果你的函数很大,并且你用各种不同的数组大小调用它,这可能代价很高。你可以像这样使用它作为一个函数的包装器:

void myFunction(double * arr, int R, int C)
{
    arr[x * C + y] = 5;
    etc...
}

它将数组视为一维,并使用算术计算出索引的偏移量。在这种情况下,你可以这样定义模板:

template<int C, int R>
void myFunction(double (&myArray)[R][C])
{
    myFunction(*myArray, R, C);
}

尽管表面上看,double**隐含的数据结构与固定c数组(double[][])的数据结构根本不兼容。 问题是这两种方法都是C(或c++)中处理数组的流行(尽管)被误导的方法。 参见https://www.fftw.org/fftw3_doc/Dynamic-Arrays-in-C_002dThe-Wrong-Way.html

如果你不能控制代码的任何一部分,你需要一个翻译层(这里称为adapt),解释在这里:https://c-faq.com/aryptr/dynmuldimary.html

您需要生成一个指针的辅助数组,指向c数组的每一行。

#include<algorithm>
#include<cassert>
#include<vector>

void myFunction(double** myArray) {
    myArray[2][3] = 5;
}

template<std::size_t N, std::size_t M>
auto adapt(double(&Carr2D)[N][M]) {
    std::array<double*, N> ret;
    std::transform(
        std::begin(Carr2D), std::end(Carr2D),
        ret.begin(),
        [](auto&& row) { return &row[0];}
    );
    return ret;
}

int main() {
    double anArray[10][10];

    myFunction( adapt(anArray).data() );

    assert(anArray[2][3] == 5);
}

(参见工作代码:https://godbolt.org/z/7M7KPzbWY)

如果它看起来像是一场灾难,那是因为,正如我所说,这两种数据结构从根本上是不兼容的。


如果可以控制代码的两端,那么现在最好使用现代(或半现代)数组库,比如Boost。MultiArray,提振。uBLAS,特征或多。 如果数组要小,你有“小”数组库,例如在Eigen内,或者如果你不能负担任何依赖,你可以尝试简单的std::array<std::array<double, N>, M>。

使用Multi,你可以简单地这样做:

#include<multi/array.hpp>

#include<cassert>

namespace multi = boost::multi;

template<class Array2D>
void myFunction(Array2D&& myArray) {
    myArray[2][3] = 5;
}

int main() {
    multi::array<double, 2> anArray({10, 10});

    myFunction(anArray);

    assert(anArray[2][3] == 5);
}

(工作代码:https://godbolt.org/z/7M7KPzbWY)

令人惊讶的是还没有人提到这一点,但是您可以简单地在任何支持[][]语义的2D上创建模板。

template <typename TwoD>
void myFunction(TwoD& myArray){
     myArray[x][y] = 5;
     etc...
}

// call with
double anArray[10][10];
myFunction(anArray);

它适用于任何2D“类数组”数据结构,例如std::vector<std::vector<T>>,或者用户定义的类型以最大限度地重用代码。

如果你想将int的[2][3]传递给void func(int** pp),你需要以下辅助步骤。

int a[2][3];
int* p[2] = {a[0],a[1]};
int** pp = p;

func(pp);

由于第一个[2]可以隐式指定,因此可以进一步简化为。

int a[][3];
int* p[] = {a[0],a[1]};
int** pp = p;

func(pp);

传递多维数组的一个重要的事情是:

第一个数组维度不需要指定。 必须指定第二个维度(任何其他维度)。

1.当全局只有第二个维度可用时(作为宏或全局常量)

const int N = 3;

void print(int arr[][N], int m)
{
int i, j;
for (i = 0; i < m; i++)
  for (j = 0; j < N; j++)
    printf("%d ", arr[i][j]);
}

int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr, 3);
return 0;
}

2.使用单个指针: 在此方法中,当传递给函数时,必须对2D数组进行类型转换。

void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
  for (j = 0; j < n; j++)
    printf("%d ", *((arr+i*n) + j));
 }

int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;

// We can also use "print(&arr[0][0], m, n);"
print((int *)arr, m, n);
return 0;
}