地图的二维数组到一维列C [英] Map a 2D array onto a 1D array C

查看:247
本文介绍了地图的二维数组到一维列C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重新present一个二维数组与一维数组。甲功能将通过两个indicies(X,Y)并将该值存储。这两个indicies将重新present一维数组的一个元素,并相应地设置它。我所知道的一维数组需要(arrayWidth * arrayHeight)的大小,但我不知道如何设置每个元素。例如,如果我通过它(2,4,3)我如何区分,从(4,2,3)?我尝试设置阵列的X * Y,但你可以看到2 * 4,4 * 2将导致阵列,在同一个地点,当我需要他们有所不同。 这可能是不清楚的,如果您需要更多的信息或澄清只是要求。我希望你能有所帮助。

I want to represent a 2D array with a 1D array. A function will pass the two indicies (x,y) and the value to store. These two indicies would represent a single element of a 1D array, and set it accordingly. I know the 1D array needs to be (arrayWidth *arrayHeight) in size, but I don't know how to set each element. For example if I passed it (2,4,3) how do i distinguish that from (4,2,3)? I tried setting the array as the x*y, but as you can see 2*4 and 4*2 would result in the same spot in the array, when i need them to be different. This might be unclear, if you need more information or clarification just ask. I hope you can help.

推荐答案

您需要确定数组元素是否将被存储在行顺序或列顺序,然后将它保持一致。 <一href="http://en.wikipedia.org/wiki/Row-major_order">http://en.wikipedia.org/wiki/Row-major_order

You need to decide whether the array elements will be stored in row order or column order and then be consistent about it. http://en.wikipedia.org/wiki/Row-major_order

C语言使用行为了多维数组

The C language uses row order for Multidimensional arrays

要模拟这一与一维数组,由宽度乘以行索引,从而增加了列索引:

To simulate this with a single dimensional array, you multiply the row index by the width, and add the column index thus:

 int array[width * height];

 int SetElement(int row, int col, int value)
 {
    array[width * row + col] = value;  
 }

这篇关于地图的二维数组到一维列C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆