C语言 将二维数组映射到一维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2151084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map a 2D array onto a 1D array
提问by Blackbinary
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 have the size of arrayWidth × arrayHeight, but I don't know how to set each element.
我想用一维数组表示一个二维数组。函数将传递两个索引 (x,y) 和要存储的值。这两个索引将代表一维数组的单个元素,并相应地设置它。我知道一维数组需要有arrayWidth × arrayHeight的大小,但我不知道如何设置每个元素。
For example, how do I distinguish (2,4,3) from (4,2,3)? I tried setting the array as the x*y, but 2*4 and 4*2 would result in the same spot in the array and I need them to be different.
例如,我如何区分 (2,4,3) 和 (4,2,3)?我尝试将数组设置为 x*y,但 2*4 和 4*2 会导致数组中的相同点,我需要它们不同。
回答by John Knoeller
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
您需要决定数组元素是按行顺序还是按列顺序存储,然后保持一致。http://en.wikipedia.org/wiki/Row-major_order
The C language uses row order for Multidimensional arrays
C 语言对多维数组使用行顺序
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;
}
回答by AnT
The typical formula for recalculation of 2D array indices into 1D array index is
将二维数组索引重新计算为一维数组索引的典型公式是
index = indexX * arrayWidth + indexY;
Alternatively you can use
或者你可以使用
index = indexY * arrayHeight + indexX;
(assuming that arrayWidthis measured along X axis, and arrayHeightalong Y axis)
(假设arrayWidth沿 X 轴和arrayHeightY 轴测量)
Of course, one can come up with many different formulae that provide alternative unique mappings, but normally there's no need to.
当然,人们可以想出许多不同的公式来提供替代的唯一映射,但通常没有必要。
In C/C++ languages built-in multidimensional arrays are stored in memory so that the last index changes the fastest, meaning that for an array declared as
在 C/C++ 语言中,内置的多维数组存储在内存中,因此最后一个索引变化最快,这意味着对于声明为的数组
int xy[10][10];
element xy[5][3]is immediately followed by xy[5][4]in memory. You might want to follow that convention as well, choosing one of the above two formulae depending on which index (X or Y) you consider to be the "last" of the two.
elementxy[5][3]紧跟xy[5][4]在内存中。您可能还想遵循该约定,根据您认为哪个索引(X 或 Y)是两者中的“最后一个”,选择上述两个公式之一。
回答by Kornel Kisielewicz
Example : we want to represent an 2D array of SIZE_X and SIZE_Y size. That means that we will have MAXY consecutive rows of MAXX size. Hence the set function is
示例:我们要表示 SIZE_X 和 SIZE_Y 大小的二维数组。这意味着我们将有 MAXY 个连续行的 MAXX 大小。因此集合函数是
void set_array( int x, int y, int val ) { array[ x * SIZE_Y + y ] = val; }
The get would be:
得到将是:
int get_array( int x, int y ) { return array[ x * SIZE_Y + y ]; }
回答by Sammy
As other have said C maps in row order
正如其他人所说的 C 映射按行顺序
#include <stdio.h>
int main(int argc, char **argv) {
int i, j, k;
int arr[5][3];
int *arr2 = (int*)arr;
for (k=0; k<15; k++) {
arr2[k] = k;
printf("arr[%d] = %2d\n", k, arr2[k]);
}
for (i=0; i<5; i++) {
for (j=0; j< 3; j++) {
printf("arr2[%d][%d] = %2d\n", i, j ,arr[i][j]);
}
}
}
Output:
输出:
arr[0] = 0
arr[1] = 1
arr[2] = 2
arr[3] = 3
arr[4] = 4
arr[5] = 5
arr[6] = 6
arr[7] = 7
arr[8] = 8
arr[9] = 9
arr[10] = 10
arr[11] = 11
arr[12] = 12
arr[13] = 13
arr[14] = 14
arr2[0][0] = 0
arr2[0][1] = 1
arr2[0][2] = 2
arr2[1][0] = 3
arr2[1][1] = 4
arr2[1][2] = 5
arr2[2][0] = 6
arr2[2][1] = 7
arr2[2][2] = 8
arr2[3][0] = 9
arr2[3][1] = 10
arr2[3][2] = 11
arr2[4][0] = 12
arr2[4][1] = 13
arr2[4][2] = 14
回答by Anycorn
using row major example:
使用行主要示例:
A(i,j) = a[i + j*ld]; // where ld is the leading dimension
// (commonly same as array dimension in i)
// matrix like notation using preprocessor hack, allows to hide indexing
#define A(i,j) A[(i) + (j)*ld]
double *A = ...;
size_t ld = ...;
A(i,j) = ...;
... = A(j,i);
回答by Mark
It's important to store the data in a way that it can be retrieved in the languages used. C-language stores in row-major order (all of first row comes first, then all of second row,...) with every index running from 0 to it's dimension-1. So the order of array x[2][3] is x[0][0], x[0][1], x[0][2], x[1][0], x[1][1], x[1][2]. So in C language, x[i][j] is stored the same place as a 1-dimensional array entry x1dim[ i*3 +j]. If the data is stored that way, it is easy to retrieve in C language.
以一种可以用所使用的语言检索数据的方式存储数据很重要。C 语言以行优先顺序存储(第一行的所有内容,然后是第二行的所有内容,...),每个索引从 0 到其维度 1。所以数组 x[2][3] 的顺序是 x[0][0], x[0][1], x[0][2], x[1][0], x[1][ 1],x[1][2]。所以在C语言中,x[i][j]和一维数组项x1dim[i*3+j]存放在同一个地方。如果数据以这种方式存储,则很容易用 C 语言检索。
Fortran and MATLAB are different. They store in column-major order (all of first column comes first, then all of second row,...) and every index runs from 1 to it's dimension. So the index order is the reverse of C and all the indices are 1 greater. If you store the data in the C language order, FORTRAN can find X_C_language[i][j] using X_FORTRAN(j+1, i+1). For instance, X_C_language[1][2] is equal to X_FORTRAN(3,2). In 1-dimensional arrays, that data value is at X1dim_C_language[2*Cdim2 + 3], which is the same position as X1dim_FORTRAN(2*Fdim1 + 3 + 1). Remember that Cdim2 = Fdim1 because the order of indices is reversed.
Fortran 和 MATLAB 是不同的。它们以列优先顺序存储(第一列的所有内容首先出现,然后是第二行的所有内容,......)并且每个索引从 1 到它的维度运行。所以索引顺序是 C 的逆序,所有的索引都大 1。如果按照 C 语言顺序存储数据,FORTRAN 可以使用 X_FORTRAN(j+1, i+1) 找到 X_C_language[i][j]。例如,X_C_language[1][2] 等于 X_FORTRAN(3,2)。在一维数组中,该数据值位于 X1dim_C_language[2*Cdim2 + 3],与 X1dim_FORTRAN(2*Fdim1 + 3 + 1) 的位置相同。请记住 Cdim2 = Fdim1,因为索引的顺序是相反的。
MATLAB is the same as FORTRAN. Ada is the same as C except the indices normally start at 1. Any language will have the indices in one of those C or FORTRAN orders and the indices will start at 0 or 1 and can be adjusted accordingly to get at the stored data.
MATLAB 与 FORTRAN 相同。除了索引通常从 1 开始,Ada 与 C 相同。任何语言都将在那些 C 或 FORTRAN 顺序之一中具有索引,并且索引将从 0 或 1 开始,并且可以相应地进行调整以获取存储的数据。
Sorry if this explanation is confusing, but I think it is accurate and important for a programmer to know.
对不起,如果这个解释令人困惑,但我认为程序员知道它是准确和重要的。
回答by Arthur Kalliokoski
You should be able to access the 2d array with a simple pointer in place. The array[x][y] will be arranged in the pointer as p[0x * width + 0y][0x * width + 1y]...[0x * width + n-1y][1x * width + 0y] etc.
您应该能够使用一个简单的指针来访问二维数组。数组[x][y] 将在指针中排列为 p[0x * width + 0y][0x * width + 1y]...[0x * width + n-1y][1x * width + 0y] 等.

