多维数组 [英] Multi-dimensional array
本文介绍了多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个具有参数的函数,该参数是一个多维数组,具有两个用户指定的维度,例如
int function(int a,int b,int array [a] [b])
/ pre>
{
...
}
我如何在C ++中这样做?
解决方案编译时的维度是否已知?在这种情况下,将它们转换为模板参数,并通过引用传递数组:
template< int a,int b&
pre>
int function(int(& array)[a] [b])
{
...
}
客户端代码示例:
int x [3] 7];
function(x);
int y [6] [2];
function(y);
I need to create a function that has a parameter which is a multi-dimensional array with two dimensions being user-specified, e.g.
int function(int a, int b, int array[a][b]) { ... }
How would I do that in C++ ?
解决方案Are the dimensions known at compile-time? In that case, turn them into template parameters and pass the array by reference:
template<int a, int b> int function(int(&array)[a][b]) { ... }
Example client code:
int x[3][7]; function(x); int y[6][2]; function(y);
这篇关于多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文