Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可以说,我有一个 MxN 数组:int *b;以及int **c;在哪里
int *b;
int **c;
b
c
我知道基本上我会这样做:
j = index / N; i = index - (j * M);
为了将一维索引转换为二维坐标但有问题如何实现这两种情况,1)和2)?
令 W 为 2D 数组的宽度,H 为高度。然后假设行主要布局,一维索引“ix”与二维索引 [x,y] 相关,如下所示:
ix = y*w + x; y = ix / w; // implicit floor x = ix % w;
例如:
const int W = 3, H=2; int m[H][W] = {{1,2,3}, {4,5,6}}; int* oneD = &m[0][0]; assert(oneD[1*W + 2] == m[1][2]); // element 6, y=1, x=2