如何在 C/C++ 中获取多维数组的列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15258084/
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
How to get column of a multidimensional array in C/C++?
提问by Qbik
int matrix[9][9],*p;
p=matrix[0];
this works and gives first row of matrix
, but how to get first column of matrix
I've tried p=matrix[][0];
? Also I don't understand why below code gets compiler error ?
这有效并给出了第一行matrix
,但是如何获得matrix
我尝试过的第一列p=matrix[][0];
?另外我不明白为什么下面的代码会出现编译器错误?
int matrix[9][9],p[9]; // it looks really ugly, byt why it doesn't work ?
p=matrix[0]; // compiler gives "invalid array assigment"
is it because multidimensional arrays are arrays of arrays - and we should interpret matrix[i][j]
as j-th element of i-th nested array ?
是不是因为多维数组是数组的数组 - 我们应该解释matrix[i][j]
为第 i 个嵌套数组的第 j 个元素?
回答by meyumer
In C/C++, multidimensional arrays are actually stored as one dimensional arrays (in the memory). Your 2D matrix is stored as a one dimensional array with row-first ordering. That is why getting a column out of it is not easy, and not provided by default. There is no contiguous array in the memory that you can get a pointer to which represents a column of a multidimensional array.See below:
在 C/C++ 中,多维数组实际上存储为一维数组(在内存中)。您的二维矩阵存储为行优先排序的一维数组。这就是为什么从中取出一列并不容易,并且默认情况下不提供。内存中没有连续的数组,您可以获得表示多维数组列的指针。见下文:
When you do p=matrix[0]
, you are just getting the pointer to the first element matrix[0][0]
, and that makes you think that you got the pointer to first row. Actually, it is a pointer to the entire contiguous array that holds matrix
, as follows:
当你这样做时p=matrix[0]
,你只是得到了指向第一个元素的指针matrix[0][0]
,这让你认为你得到了指向第一行的指针。实际上,它是指向包含 的整个连续数组的指针matrix
,如下所示:
matrix[0][0]
matrix[0][1]
matrix[0][2]
.
.
matrix[1][0]
matrix[1][1]
matrix[1][2]
.
.
matrix[8][0]
matrix[8][1]
matrix[8][2]
.
.
matrix[8][8]
As seen above, the elements of any given column are separated by other elements in the corresponding rows.
如上所示,任何给定列的元素都被相应行中的其他元素分隔。
So, as a side note, with pointer p
, you can walk through the entire 81 elements of your matrix if you wanted to.
因此,作为旁注,使用 pointer p
,如果您愿意,您可以遍历矩阵的全部 81 个元素。
回答by meyumer
You can get the first column using a loop like
您可以使用类似的循环获取第一列
for(int i = 0; i < 9; i++)
{
printf("Element %d: %d", i, matrix[i][0]);
}
I think the assignment doesn't work properly because you're trying to assign something's that's not an address to a pointer.
我认为分配不能正常工作,因为您试图将不是地址的东西分配给指针。
(Sorry this is c code)
(对不起,这是c代码)
回答by AxelOmega
There is no difference between specifying matrix[81]
or matrix[9][9]
指定matrix[81]
或matrix[9][9]
matrix[r][c]
simply means the same as matrix[9*r+c]
matrix[r][c]
只是意味着相同 matrix[9*r+c]
There are other containers better suited fort multidimensional arrays like boost::multi_array
还有其他容器更适合多维数组,例如 boost::multi_array
http://www.boost.org/doc/libs/1_53_0/libs/multi_array/doc/index.html
http://www.boost.org/doc/libs/1_53_0/libs/multi_array/doc/index.html
Think of the bare array just like allocating a contiguous piece of memory. You, the programmer then has to handle this piece of memory yourself. The bare name of the array, e.g. matrix
is a pointer to the first element of this allocated piece of memory. Then *(matrix+1)
is the same as matrix[0][1]
or matrix[1]
.
把裸数组想象成分配一块连续的内存。你,程序员然后必须自己处理这块内存。数组的裸名,例如,matrix
是指向该分配内存的第一个元素的指针。then*(matrix+1)
与matrix[0][1]
or相同matrix[1]
。
回答by zzk
p is an array of int, matrix[0] is a pointer..
p 是一个 int 数组,matrix[0] 是一个指针..
回答by Neil
matrix
itself is the nearest thing you can get to a column of the array, inasmuch as (matrix + 1)[0][0]
is the same as matrix[1][0]
.
matrix
本身是最接近数组列的对象,因为(matrix + 1)[0][0]
与matrix[1][0]
.
回答by Thomas Matthews
If you want your matrix to contiguous locations, declare it as a one dimensional array and perform the row and column calculations yourself:
如果您希望矩阵位于连续位置,请将其声明为一维数组并自行执行行和列计算:
int contiguous_matrix[81];
int& location(int row, int column)
{
return contiguous_matrix[row * 9 + column];
}
You can also iterate over each column of a row:
您还可以遍历一行的每一列:
typedef void (*Function_Pointer)(int&);
void Column_Iteration(Function_Pointer p_func, int row)
{
row = row * MAXIMUM_COLUMNS;
for (unsigned int column = 0; column < 9; ++column)
{
p_func(contiguous_matrix[row + column]);
}
}
回答by Georgi
For static declared arrays you can access them like continuous 1D array, p = matrix[0]
will give you the 1st column of 1st row. Then the 1D array can be accessed like p[i]
, *(p+i)
, or p[current_raw * raw_size + current_column)
.
对于静态声明的数组,您可以像访问连续一维数组一样访问它们,p = matrix[0]
将为您提供第一行的第一列。然后1D阵列可以像访问p[i]
,*(p+i)
或p[current_raw * raw_size + current_column)
。
The things are getting tricky if a 2D array is represented with **p
as it will be interpreted as an array of pointers to 1D arrays.
如果用 2D 数组表示,事情会变得棘手,**p
因为它将被解释为指向 1D 数组的指针数组。