如何通过一个多维数组给函数没有在C ++中的内部尺寸? [英] How to pass a multidimensional array to a function without inner dimension in c++?

查看:106
本文介绍了如何通过一个多维数组给函数没有在C ++中的内部尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有一种方法说通过这个例如双MYARRAY [] [2] = {{0.1,0.8},{0.4,0.6}} 到这样的功能无效MyFunction的(双myarray的[] []); ,没有说这个无效MyFunction的(双myarray的[] [2] ); 。我需要这个,因为我希望我的功能,能够处理例如与不同的内部尺寸数组:双MYARRAY [] [3] = {{0.1,0.8},{0.4,0.6},{0.3 ,0.9}} 。要尽量把它传递给一个函数,因为我知道我将不得不改变MyFunctions的参数,以的MyFunction(myarray的[] [3]); 。从我读,我不知道这是可能的,因此,如果它不是,那么有没有这样做的其他方式?

I was wondering if there was a way to say pass this for example double MyArray[][2] = {{0.1,0.8},{0.4,0.6}} to a function like this void MyFunction(double myArray[][]);, without saying this void MyFunction(double myArray[][2]);. I need this because I want my function to be able to handle arrays with different inner dimensions for example: double MyArray[][3] = {{0.1,0.8},{0.4,0.6},{0.3,0.9}}. To pass this to a function as far as I know I would have to change MyFunctions's parameters to MyFunction(myArray[][3]);. From what I have read I don't know if this is possible, so if it is not then is there some other way of doing this?

推荐答案

您可以通过引用传递一个任意的二维数组,如果你可以改变的MyFunction 来:

You can pass an arbitrary 2d array by reference if you could change MyFunction to:

template<std::size_t N, std::size_t M>
void MyFunction(double (&myArray)[N][M]) {
  // ...
}

此方式,您也将有数组的大小。

This way you would also have dimensions of the array.

这篇关于如何通过一个多维数组给函数没有在C ++中的内部尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆