Chr*_*cle 1 c++ arrays multidimensional-array
我有一个网格,我想传递给另一个显示它的函数.但是,我似乎无法弄清楚我将如何声明该函数,多维数组通过引用传递给该函数.
void foo(bool[][]&); //INCORRECT: how is a correct way to declare this function?
// rest of code :
int main(){
bool grid[50][50] = {false};
foo(grid);
return 0;
}
void foo(bool& grid[][]){
// do things
}
Run Code Online (Sandbox Code Playgroud)
这应该是一个基本问题,但我在寻找解决方案时遇到了很多麻烦.
对2D数组类型的引用如下所示:
T (&)[N][M]
Run Code Online (Sandbox Code Playgroud)
所以你要:
void foo(bool(&)[50][50]);
Run Code Online (Sandbox Code Playgroud)
请注意,必须指定尺寸.对于函数定义,它看起来像:
void foo(bool (&grid)[50][50]) {
Run Code Online (Sandbox Code Playgroud)
如果您需要能够将该功能用于各种尺寸的2D阵列,您可以将其作为尺寸上的模板:
template <std::size_t N, std::size_t M>
void foo(bool(&)[N][M]);
Run Code Online (Sandbox Code Playgroud)
将为您传递给它的每个数组大小实例化模板.
| 归档时间: |
|
| 查看次数: |
76 次 |
| 最近记录: |