将参数传递给函数的问题
我正在使用 C++ 并发现一个问题。我想将参数传递给函数。参数必须是二维数组。当我尝试这样做时,出现两个错误:
初始化值太多
并且
初始化无法从初始化列表转换为 size_t**
我该如何解决这个问题?我尝试过将其更改为 5x5 矩阵,但效果并不好。
size_t** matrix =
{
{1, 16, 20, 23, 25},
{6, 2, 17, 21, 24},
{10, 7, 3, 18, 22},
{13, 11, 8, 4, 19},
{15, 14, 12, 9, 5},
};
set<bool> set1 = iterateover(matrix);
功能:
std::set<bool> iterateover(size_t **arrayy)
I'm working with C++ and found a problem. I want to pass an argument to a function. The argument must be a 2d array. When I try to do it, I get 2 errors:
Too many initializer values
and
initializing cannnot convert from initializer list to size_t**
How do I fix this? I've tried with changing it as 5x5 matrix, but it doesn't make it good.
size_t** matrix =
{
{1, 16, 20, 23, 25},
{6, 2, 17, 21, 24},
{10, 7, 3, 18, 22},
{13, 11, 8, 4, 19},
{15, 14, 12, 9, 5},
};
set<bool> set1 = iterateover(matrix);
The function:
std::set<bool> iterateover(size_t **arrayy)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
size_t** 矩阵
定义一个指向size_t
的指针。数组不是指针。 它可以衰减为指针,但在二维数组的情况下,它衰减为指向一维数组的指针,而不是指向指针的指针。我能想到的最接近你所追求的是
size_t** matrix
defines a pointer to a pointer to asize_t
. An array is not a pointer. It can decay to a pointer, but in the case of a 2D array, it decays to a pointer to a 1D array, not to a pointer to a pointer.The closest thing I can think of to what you seem to be after is
您可以将
iteratreOver
设为一个函数模板,它可以通过引用获取 2D 数组,如下所示。您可以根据您的需要对该函数进行其他更改,因为从问题中并不清楚您的iterateover
函数的作用。我刚刚打印了二维数组内的所有元素。上述程序的输出可以在此处查看:
You can make
iteratreOver
a function template which can take a 2D array by reference, as shown below. You can make additional changes to the function according to your needs since it is not clear from the question what youriterateover
function does. I have just printed all the elements inside the 2D array.The output of the above program can be seen here: