简体   繁体   English

如何将动态多维数组传递给函数?

[英]How can I pass a dynamic multidimensional array to a function?

How can I pass a multidimensional array to a function in C/C++ ? 如何将多维数组传递给C / C ++中的函数?

The dimensions of array are not known at compile time 数组的维度在编译时是未知的

A pointer to the start of the array along with the dimensions - then do the array arithmetic in the function is the most common solution. 指向数组开头和维度的指针 - 然后在函数中执行数组运算是最常见的解决方案。

Or use boost 或者使用提升

Passing the array is easy, the hard part is accessing the array inside your function. 传递数组很容易,困难的部分是访问函数内的数组。 As noted by some of the other answers, you can declare the parameter to the function as a pointer and also pass the number of elements for each dim of the array. 正如其他一些答案所述,您可以将函数的参数声明为指针,并传递数组每个dim的元素数。

#define xsize 20
#define ysize 30
int array[xsize][ysize];
void fun(int* arr, int x, int y)
{
 // to access element 5,20
 int x = arr[y*5+20];
}

fun(array, xsize, ysize);

Of course, I've left out the whole business of allocating the array (since it isn't known what its size will be, you can't really use #defines (and some say they're bad anyhow) 当然,我遗漏了分配数组的整个业务(因为不知道它的大小是什么,你不能真正使用#defines(有人说它们无论如何都是坏的)

使用矢量矢量,您可以传递矢量。

You could pass a pointer and sizes, or use a std::vector . 您可以传递指针和大小,或使用std::vector But the "real" solution is with a template: 但“真正的”解决方案是使用模板:

template <size_t N, size_t M>
void foo(int (&pArray)[N][M]);

This function template accepts a N by M array of ints, by reference. 此函数模板通过引用接受N乘M的int数组。 Note this is a function template, not a function, so you do get a different instantiated function per array type. 请注意,这是一个函数模板,而不是函数,因此每个数组类型都会获得不同的实例化函数。

我认为这是一个GCC扩展(或一个非常现代的C功能),但它可以非常方便:

void foo(int bar[n][m], int n, int m) {...}

You can pass the pointer to initial memory location of your multi dimension array. 您可以将指针传递到多维数组的初始内存位置。 you should also pass the size of array ie limit of each dimension. 你还应该传递数组的大小,即每个维度的限制。

ie

int var [x][y][z];
func (var, x, y, z);

function definintion: 功能定义:

void func (int*, int, int, int);

I'm just summarizing the options from other posts. 我只是总结了其他帖子的选项。

If the number of dimensions (the N as in N-dimensional array) is unknown, the only way is to use a C++ multidimensional array class. 如果维数(N维数组中的N)未知,唯一的方法是使用C ++多维数组类。 There are several publicly available implementations, from Boost or other libraries. Boost或其他库有几种公开可用的实现。 See Martin Beckett's post. 请参阅Martin Beckett的帖子。

If the number of dimensions is known but the array size is dynamic, see Tom's answer for accessing an array element (converting multi index into element pointer). 如果已知维数但数组大小是动态的,请参阅Tom的访问数组元素答案(将多索引转换为元素指针)。 The array itself will have to be allocated with malloc or new. 数组本身必须使用malloc或new分配。

If you are writing the multidimensional array class yourself, you'll need to know about Row-major-order , Column-major-order, etc. 如果您自己编写多维数组类,则需要了解Row-major-order ,Column-major-order等。

Namely, if the array dimensios is (Size1, Size2, Size3, ..., SizeN) , then: 即,如果数组维度是(Size1, Size2, Size3, ..., SizeN) ,那么:

  • The number of elements in the array is (Size1 * Size2 * Size3 * ... * SizeN) 数组中的元素数量为(Size1 * Size2 * Size3 * ... * SizeN)
  • The memory needed is sizeof(value_type) * numOfElements 所需的内存是sizeof(value_type) * numOfElements
  • To access the element (index1, index2, index3, ..., indexN) , use 要访问元素(index1, index2, index3, ..., indexN) ,请使用
    • ptr[ index1 + (Size1 * index2) + (Size1 * Size2 * index3) + ... ] assuming the first array index is the fastest-moving dimension ptr[ index1 + (Size1 * index2) + (Size1 * Size2 * index3) + ... ]假设第一个数组索引是移动最快的维度

Section 3.4 on this page addresses your question: 本页第3.4节解决了您的问题:

http://www.programmersheaven.com/2/Pointers-and-Arrays-page-2 http://www.programmersheaven.com/2/Pointers-and-Arrays-page-2

Of course variable-length arrays were not present in C until C99 and as far as I know they are not present in C++. 当然,在C99之前C中不存在可变长度数组,据我所知它们在C ++中不存在。 Also, MSVC does not implement/support C99. 此外,MSVC不实现/支持C99。

A simple method is to flatten the array and iterate using dimensions. 一种简单的方法是展平数组并使用维度进行迭代。

#include <stdio.h>

void print_array(int *arr,int row,int col)
{
     int i,j;
     for(i=0;i<row;i++){           
         for(j=0;j<col;j++){
             printf("%d ",*(arr+i*col+j));
         }
         printf("\n");
     }
}

int main()
{
int a[2][3] = {{1,0,2},{-1,3,1}};
int b[4] = {1,2,3,34};
print_array(a,2,3);
return 0;
}

This technique works but flattening array might prevent compiler optimizations which in turn might result in slow execution. 此技术有效,但flattening数组可能会阻止编译器优化,这反过来可能导致执行缓慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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