c++ 的问题使用二维数组引用函数
我试图获取这个矩阵并在另一个函数中将其打印出来。问题是这是不可能的。我尝试用 ** 和 * 来解决它,但我只得到了地址而不是值,但我无法得到矩阵 5x5 中的正常值。有人可以解释我以错误的方式做了什么吗?
size_t matrix[5][5] =
{
{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((size_t**)matrix)
std::set<bool> iterateover(size_t** array)
size_t numberofrows = sizeof(**arrayy) / sizeof(arrayy[0]);
size_t numberofkols = sizeof(*arrayy[0]) / sizeof(arrayy[0][0]);
std::set < bool >myset;
for (size_t i = 0; i < numberofrows; i++)
{
for (size_t j = 0; j < numberofkols; j++)
{
std::cout << arrayy[i][j] << std::endl;
return myset;
I'm trying to get this matrix and print it out in the another function. The problem is that is impossible. I tried to solve it with ** and * but I got only the adress not the value but I cannot get the normal values as this in matrix 5x5. Can someone explain me what I do in a wrong way?
size_t matrix[5][5] =
{
{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((size_t**)matrix)
std::set<bool> iterateover(size_t** array)
size_t numberofrows = sizeof(**arrayy) / sizeof(arrayy[0]);
size_t numberofkols = sizeof(*arrayy[0]) / sizeof(arrayy[0][0]);
std::set < bool >myset;
for (size_t i = 0; i < numberofrows; i++)
{
for (size_t j = 0; j < numberofkols; j++)
{
std::cout << arrayy[i][j] << std::endl;
return myset;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
iteratreOver
设为一个函数模板,它可以通过引用获取 2D 数组,如下所示。上述程序的输出可以在此处查看:
You can make
iteratreOver
a function template which can take a 2D array by reference, as shown below.The output of the above program can be seen here:
你得到的行数、列数不正确,应该是这样的:
并且你想打印到其他函数,让使用
在以下位置测试的模板: https://godbolt.org/z/vshev1e17
you get row, col number incorrectly,it should be like this:
and you want to print out to other function lets use template
tested at : https://godbolt.org/z/vshev1e17
如果你想在 fun 中定义它并在另一个函数中打印它,你需要动态而不是静态地执行它,或者你可以全局定义它并从你定义的任何函数访问它
检查这个从函数返回二维数组
if u want to define it in fun and print it in another function u need to do it dynamic not static or u can define it globally and access it from any function u defined
check this Return a 2d array from a function