简体   繁体   English

在函数中修改二维数组

[英]modifying 2d array in a function

So I've written a function that turns a 2D array 90 degrees and I've been told on IRC that I can't pass 2D array by reference (for example void test(char A[][10]&)) and that I just should pass my array the usual way, however when I do that, this function doesn't change the actual array. 因此,我编写了一个将2D数组旋转90度的函数,并在IRC上被告知无法通过引用传递2D数组(例如void test(char A [] [10]&)),并且我只是应该以通常的方式传递数组,但是当我这样做时,此函数不会更改实际的数组。 So how do I modify my original array in a function ? 那么,如何修改函数中的原始数组呢?

void one(char A[][10], int N)
{
    char B [10][10];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            B[j][N-i-1] = A[i][j];
    A = B;
}

A = B ; doesn't copy the elements of array B to A permanently. 不会将数组B的元素永久复制到A。 It's an invalid assignment to change to elements of A permanently. 永久更改为A元素是无效的分配。 A retains it's original values upon function return. 函数返回时, A将保留其原始值。 You need to do a member wise copy to permanently copy the elements of B to A . 您需要进行成员明智的复制才能将B的元素永久复制到A

When you pass an array (such as char A[][10] ), you are actually passing a pointer to the original array, so doing A = B makes A point to B and doesn't change the original array. 当您传递数组(例如char A[][10] )时,实际上是在传递指向原始数组的指针,因此执行A = B使A指向B且不会更改原始数组。 Instead, you can use a function such as memcpy to actually copy the contents of B to A : 相反,您可以使用诸如memcpy的功能将B的内容实际复制到A

memcpy(A, B, sizeof(B));

Read suggested by @FredOverflow link: How do I use arrays in C++? 阅读@FredOverflow链接建议的内容: 如何在C ++中使用数组? .

To rotate 90° clock-wise NxN array you could divide the task in two smaller steps: 要沿顺时针方向旋转90°NxN阵列,可以将任务分成两个较小的步骤:

  • flip the matrix in up/down direction 上下翻转矩阵
  • transpose it 转置它

void rot90cw(char A[][N]) {
  // flip in up/down direction (swap rows)
  for (int i = 0; i < N/2; i++)
    std::swap_ranges(&A[i][0], &A[i][0] + N, &A[N-i-1][0]);

  // transpose (swap top-right and bottom-left triangles)
  for (int i = 0; i < N-1; i++)
    for (int j = i+1; j < N; j++)
      std::swap(A[i][j], A[j][i]);
}

I've used swap() and swap_ranges() to perform operations inplace. 我使用了swap()swap_ranges()来就地执行操作。

Example

// -*- coding: utf-8 -*-
#include <algorithm>
#include <iostream>

namespace {
  const int N = 3;

  // rotate 90° clock-wise
  void rot90cw(char A[][N]) {
      // ... defined above
  }

  void print(char a[][N]) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        std::cout << a[i][j];
      std::cout << '\n';
    }
    std::cout << std::endl;
  }
}

int main() {
  char a[][N] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
  print(a);
  rot90cw(a);
  std::cout << "Rotated 90° clock-wise:" << std::endl; //note: utf-8
  print(a);
}

Output 输出量

abc
def
ghi

Rotated 90° clock-wise:
gda
heb
ifc

Arrays don't work like that in C++. 数组在C ++中不能像这样工作。 When you pass an array in a function, you are passing a pointer to the first element and nothing more, so what you are doing is creating a locally defined array B , then setting the pointer passed into your function to point to the head of the B array. 当您在函数中传递数组时,您将传递指向第一个元素的指针,仅此而已,因此您要做的是创建本地定义的数组B ,然后将传递给函数的指针设置为指向函数的头部。 B数组。 At no point does the memory assigned to your original A actually change. 分配给原始A的内存实际上丝毫没有改变。 Then when the function returns, the A pointer from your function is discarded, leaving your original A array untouched. 然后,当函数返回时,函数的A指针将被丢弃,而原始A数组将保持不变。 If you want to modify an array passed as an argument to a function, you will have to modify the elements directly. 如果要修改作为参数传递给函数的数组,则必须直接修改元素。

What you are trying to pass the array by reference and change that reference. 您尝试通过引用传递数组并更改该引用的内容。 This works, but is not necessary. 这有效,但不是必需的。

You can modify the array directly element by element: 您可以按元素直接修改数组:

void one(char A[][10], int N)
{
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            char b = A[j][N-i-1];
            A[j][N-i-1] = A[i][j];
            A[i][j] = b;    
        }
}

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

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