将参数传递给函数的问题

发布于 2025-01-13 14:13:29 字数 513 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

小嗲 2025-01-20 14:13:29

size_t** 矩阵 定义一个指向size_t 的指针。数组不是指针。 它可以衰减为指针,但在二维数组的情况下,它衰减为指向一维数组的指针,而不是指向指针的指针。

我能想到的最接近你所追求的是

// here be the data
size_t actual_matrix[][5] = // note: We can omit the first dimension but we cannot 
                            // omit the inner dimensions
{
    {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},
};

// an array of pointers to the rows of actual data. This 1D array of pointers will 
// decay to a size_t **
size_t * matrix[] =
{
    actual_matrix[0],
    actual_matrix[1],
    actual_matrix[2],
    actual_matrix[3],
    actual_matrix[4],
};
// now we have the correct type to use with iterateover
std::set<bool> set1 = iterateover(matrix);

size_t** matrix defines a pointer to a pointer to a size_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

// here be the data
size_t actual_matrix[][5] = // note: We can omit the first dimension but we cannot 
                            // omit the inner dimensions
{
    {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},
};

// an array of pointers to the rows of actual data. This 1D array of pointers will 
// decay to a size_t **
size_t * matrix[] =
{
    actual_matrix[0],
    actual_matrix[1],
    actual_matrix[2],
    actual_matrix[3],
    actual_matrix[4],
};
// now we have the correct type to use with iterateover
std::set<bool> set1 = iterateover(matrix);
︶葆Ⅱㄣ 2025-01-20 14:13:29

我想将参数传递给函数。参数必须是一个二维数组。

您可以将 iteratreOver 设为一个函数模板,它可以通过引用获取 2D 数组,如下所示。您可以根据您的需要对该函数进行其他更改,因为从问题中并不清楚您的 iterateover 函数的作用。我刚刚打印了二维数组内的所有元素。

#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

I want to pass an argument to a function. The argument must be a 2d array.

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 your iterateover function does. I have just printed all the elements inside the 2D array.

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