c++ 的问题使用二维数组引用函数

发布于 2025-01-13 16:45:37 字数 788 浏览 2 评论 0原文

我试图获取这个矩阵并在另一个函数中将其打印出来。问题是这是不可能的。我尝试用 ** 和 * 来解决它,但我只得到了地址而不是值,但我无法得到矩阵 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢望 2025-01-20 16:45:37

您可以将 iteratreOver 设为一个函数模板,它可以通过引用获取 2D 数组,如下所示。

#include <iostream>
template<typename T,std::size_t N, std::size_t M>
void iterateOver(T (&arr)[N][M])
{
    for(std::size_t i= 0; i < N; ++i)
    {
        for(std::size_t j = 0; j < M; ++j)
        {
            std::cout<<arr[i][j] <<" ";
        }
        std::cout<<std::endl;
    }
}
int main()
{
    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},
    };
    //call iterateOver by passing the matrix by reference
    iterateOver(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

You can make iteratreOver a function template which can take a 2D array by reference, as shown below.

#include <iostream>
template<typename T,std::size_t N, std::size_t M>
void iterateOver(T (&arr)[N][M])
{
    for(std::size_t i= 0; i < N; ++i)
    {
        for(std::size_t j = 0; j < M; ++j)
        {
            std::cout<<arr[i][j] <<" ";
        }
        std::cout<<std::endl;
    }
}
int main()
{
    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},
    };
    //call iterateOver by passing the matrix by reference
    iterateOver(matrix);
   
}

The output of the above program can be seen here:

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
小矜持 2025-01-20 16:45:37

你得到的行数、列数不正确,应该是这样的:

   size_t numberofrows =
        sizeof(matrix) / sizeof(matrix[0]);

    size_t numberofcols = sizeof(matrix[0]) / sizeof(matrix[0][0]);

并且你想打印到其他函数,让使用

template <typename T>
void print_matrix(T matrix, int numberofcols, int numberofrows)
{
    for (size_t i = 0; i < numberofrows; i++) {
        for (size_t j = 0; j < numberofcols; j++) {
            std::cout << matrix[i][j] <<" ";
        }
        std::cout << "\n";
    }
}

在以下位置测试的模板: https://godbolt.org/z/vshev1e17

you get row, col number incorrectly,it should be like this:

   size_t numberofrows =
        sizeof(matrix) / sizeof(matrix[0]);

    size_t numberofcols = sizeof(matrix[0]) / sizeof(matrix[0][0]);

and you want to print out to other function lets use template

template <typename T>
void print_matrix(T matrix, int numberofcols, int numberofrows)
{
    for (size_t i = 0; i < numberofrows; i++) {
        for (size_t j = 0; j < numberofcols; j++) {
            std::cout << matrix[i][j] <<" ";
        }
        std::cout << "\n";
    }
}

tested at : https://godbolt.org/z/vshev1e17

勿忘心安 2025-01-20 16:45:37

如果你想在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文