简体   繁体   English

如何传递二维数组并将其返回给函数

[英]How to pass and return 2d array to a function

I'm trying to change a 2D array values in different function so it suits my need. 我正在尝试在不同的函数中更改2D数组的值,以使其适合我的需要。 For an example in the code below I'm trying to change the Matrix to reflexive by changing the to "1" . 例如,在下面的代码中,我试图通过将Matrix更改为自反,将其更改为“ 1”。

I have an issue returning the new array and replace it with the old one in the main program so I can use it again. 我在返回新数组并将其替换为主程序中的旧数组时遇到问题,因此可以再次使用它。

int reflexive(int m, int n, int matrix[100][100])
{

 for(int i = 1;i <= m; i++)
  {
    for(int j = 1; j <= n; j++)
    {
        if(i == j)
        {
            if(matrix[i][j] != 1)
                matrix[i][j] = 1;
        }
 return matrix;
     }
   }
 }
int main()
{
    int  matrix[100][100];
    int m , n;
    for(int i = 1;i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            cin>>matrix[i][j];
        }
    }

    matrix[m][n] = reflexive(m,n,matrix);
 return 0;
}

I want to fix this code so it can return the full new 2D array to the main program. 我想修复此代码,以便它可以将完整的2D数组返回到主程序。

In your function reflexive the return statement is inside the loop. reflexive函数中,return语句位于循环内部。 This causes that the function is terminated immediately when the return statement is reached. 这导致该函数在到达return语句时立即终止。 If you would proper format your code you would be able to see this behavior easily. 如果您可以正确格式化代码,则可以轻松看到此行为。

Further note, that the first element in an array has the index 0 and the index of the last element is the the size of the array -1. 还要注意,数组中的第一个元素的索引为0,最后一个元素的索引为数组-1的大小。 This is, because the array index is the element "offset". 这是因为数组索引是元素“偏移量”。 This means that 这意味着

a[i]

and

*(a + i)

access the same array element. 访问相同的数组元素。

for(int i = 1;i <= m; i++)      // <--- should be  for(int i=0; i<m; i++)
{
    for(int j = 1; j <= n; j++) // <--- should be for(int j=0; j<n; j++)
    {
        if(i == j)
        {
            if(matrix[i][j] != 1)
                matrix[i][j] = 1;
        }
        return matrix;          // <--- here the function is terminated
    }
}

An array is passed to a function by a pointer and not by value. 数组是通过指针而不是通过值传递给函数的。 This means if a formal parameter of a function is an array, then not all the values of the actual parameter are copied to the array. 这意味着,如果函数的形式参数是数组,则并非实际参数的所有值都被复制到数组中。 There is only passed a pointer to the values of the paramter to the function. 仅传递一个指向函数参数值的指针。
Because of that you do not need any return value in your case. 因此,您的情况不需要任何返回值。

The following code should do what you want: 下面的代码应执行所需的操作:

int reflexive(int m, int n, int matrix[100][100])
{
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(i == j)
            {
                if(matrix[i][j] != 1)
                    matrix[i][j] = 1; // <----- write to the data by pointer
            }
        }
    }
}

int main()
{
    int  matrix[100][100];
    ......
    reflexive(m,n,matrix);
    ......
}

使用vector<vector<int>>并通过引用传递

Using an array here is tedious and error-prone. 在这里使用数组是乏味且容易出错的。 Therefore: 因此:

#include <vector>

std::vector<std::vector<int> > reflexive(std::vector<std::vector<int> > matrix)
{
    for(int i = 0; i < matrix.size; i++){
        for(int j = 0; j < matrix[i].size; j++){
....

int main()
{
    std::vector<std::vector<int> > matrix(100, std::vector<int>(100));
    int m , n; //<<whereever they come from I guess? Saw no cin in your code. Have no set value right now.
    for(int i = 0; i < m; i++)
    {
        for(int j =0; j < n; j++)
        {
            cin>>matrix[i][j];
        }
    }
}

or 要么

#include<array>

...

std::array<std::array<int, 100>, 100> matrix;

std::array is of fixed size, std::vector is dynamic. std::array具有固定大小, std::vector是动态的。 Also learn about std::list while you're at it. 同时,还可以了解std::list

In any case, you should find a basic tutorial that talks about them and read all of it. 无论如何,您都应该找到有关它们的基础教程并阅读所有内容。 Important standards. 重要标准。

That said, especially if you want to use dynamic sized arrays, you have no guarantee that every sub-array is of the same size ("a jagged 2d array), which is somehwat okay in a small program but should be avoided in the big picture, by using a class that ensures this property. 就是说,特别是如果您要使用动态大小的数组,则不能保证每个子数组都具有相同的大小(“ 锯齿 2d数组”),这在小程序中是可以的,但在大型程序中则应避免图片,方法是使用确保该属性的类。

In the case of a matrix, one easy option is to use a library that deals with matrices, like for example Eigen. 在矩阵的情况下,一个简单的选择是使用一个处理矩阵的库,例如Eigen。

edit: 编辑:

If at some point you need the code above in const-correct: 如果需要在const-correct中使用以上代码:

std::vector<std::vector<int> > reflexive(const std::vector<std::vector<int> >& input)
{
    std::vector<std::vector<int> > output = input;
    ....
    return output;
}

(or make it void if it is allowed to modify the original matrix) (如果允许修改原始矩阵,则使其无效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM