【问题标题】:Copy a small 2d array into a larger 2d array that has a buffer to the left and top将一个小的二维数组复制到一个较大的二维数组中,该数组的左侧和顶部都有一个缓冲区
【发布时间】:2020-04-09 19:19:27
【问题描述】:

我想将 2d 整数数组的内容放入更大的 2d 整数数组中,该数组左侧有 1 列缓冲区,顶部有 2 行缓冲区。两个数组的宽度和高度可以不同,但​​内容总是足够大以完全适合更大的数组。

更大的数组:(9 只是一个填充值)

9 9 9 9 9 9 9

9 9 9 9 9 9 9

9 0 0 0 0 0 0

9 0 0 0 0 0 0

9 0 0 0 0 0 0

9 0 0 0 0 0 0

要复制的内容:(将充满随机数)

3 1 2 3 3 5

6 7 5 5 6 5

2 1 3 5 2 2

1 3 5 2 2 5

预期输出:

9 9 9 9 9 9 9

9 9 9 9 9 9 9

9 3 1 2 3 3 5

9 6 7 5 5 6 5

9 2 1 3 5 2 2

9 1 3 5 2 2 5

【问题讨论】:

  • 可能会展平 content to copy 数组,然后循环 larger array 并将后者中的所有 0 替换为前者的顺序值。
  • 如果是 Windows,使用 [DllImport] 从 mfplat.dll 调用 MFCopyImage:docs.microsoft.com/en-us/windows/win32/api/mfapi/…
  • 对不起,我应该澄清一下内容数组每次实例化时都会有不同的值,所以我不能依赖循环来替换所有值。
  • 例如,如果数组每个像素有 1 个字节,则需要传递 pDest = 固定数组的地址 + 15,lDestStride = 7,pSrc = 固定源数组的地址,lSrcStride = 6,dwWidthInBytes=6,dwLines=5。固定两个数组时不要忘记指定 GCHandleType.Pinned,或者如果您启用了不安全代码,请使用它,它比GCHandle 稍快

标签: c# multidimensional-array


【解决方案1】:
int[,] contentToCopy = { //5x6
    {0,0,0,0,0,0},
    {0,1,1,1,1,0},
    {0,1,1,1,1,0},
    {0,1,1,1,1,0},
    {0,0,0,0,0,0}
};

int[,] arr = new int[7, 7];

for (int x = 0; x < contentToCopy.GetLength(0); x++)
{
    for (int y = 0; y < contentToCopy.GetLength(1); y++)
    {
        arr[x + 2, y + 1] = contentToCopy[x, y];
    }
}

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 2021-12-31
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多