如何使一个函数接收任意大小的多维数组中的C ++? [英] How to make a function receive multidimentional array of arbitrary size in C++?

查看:79
本文介绍了如何使一个函数接收任意大小的多维数组中的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个函数,它接受一个矩阵输入,在C ++中输出它的一些功能。但我想它在任意大小MXN矩阵工作。即我不能在函数n的值(双矩阵[] [N])的参数指定。因为n将被武断。
有什么办法,我可以通过任意MXN 2维数组给一个函数呢?
请多关照。
-indiajoe


解决方案

 模板< typename的T,为size_t W,为size_t H>
无效美孚(T(安培;数组)[W] [H]){
   //做的东西有此阵
}

I was trying to make a function which takes a matrix as input and outputs some function of it in C++. But I want it to work on arbitrary size mxn matrix. i.e. I cannot specify in the arguments of the function the values of n (double matrix[][n]) . Because n will be arbitrary. Is there any way I can pass on an arbitrary mxn 2 dimensional array to a function? Thanking you in advance. -indiajoe

解决方案

template <typename T, size_t W, size_t H>
void foo(T (&array)[W][H]) {
   // Do stuff with array here
}

这篇关于如何使一个函数接收任意大小的多维数组中的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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