[英]How to get column of a multidimensional array in C/C++?
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];
这有效并给出matrix
的第一行,但是如何获得matrix
的第一列我试过p=matrix[][0];
? ? Also I don't understand why below code gets compiler error?另外我不明白为什么下面的代码会出现编译错误?
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 个元素?
In C/C++, multidimensional arrays are actually stored as one dimensional arrays (in the memory).在 C/C++ 中,多维数组实际上存储为一维数组(在内存中)。 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:见下文:
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.当您执行p=matrix[0]
,您只是获得指向第一个元素matrix[0][0]
的指针,这让您认为您获得了指向第一行的指针。 Actually, it is a pointer to the entire contiguous array that holds matrix
, as follows:实际上,它是一个指向包含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.因此,作为旁注,如果您愿意,可以使用指针p
遍历矩阵的全部 81 个元素。
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代码)
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, eg matrix
is a pointer to the first element of this allocated piece of memory.数组的裸名,例如matrix
是指向该分配内存的第一个元素的指针。 Then *(matrix+1)
is the same as matrix[0][1]
or matrix[1]
.那么*(matrix+1)
与matrix[0][1]
或matrix[1]
。
p 是一个 int 数组,matrix[0] 是一个指针..
matrix
本身是最接近数组列的东西,因为(matrix + 1)[0][0]
与matrix[1][0]
。
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]);
}
}
For static declared arrays you can access them like continuous 1D array, p = matrix[0]
will give you the 1st column of 1st row.对于静态声明的数组,您可以像访问连续一维数组一样访问它们, p = matrix[0]
将为您提供第一行的第一列。 Then the 1D array can be accessed like p[i]
, *(p+i)
, or p[current_raw * raw_size + current_column)
.然后可以像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.如果用**p
表示二维数组,事情就会变得棘手,因为它将被解释为指向一维数组的指针数组。
I don't know if this is a efficient solution but by doing this way I'm able to get the column,我不知道这是否是一个有效的解决方案,但通过这种方式我可以得到专栏,
int arr[9][2] = { {2, 57}, {3, 66}, {4, 73}, {5, 76}, {6, 79}, {7, 81}, {8, 90}, {9, 96}, {10, 100}};
int c[18];
int co = 0;
for (auto & i : arr) {
for (int j : i) {
c[co++] = j;
}
}
for (int i = 0; i < co; ++i) {
if (i % 2 != 0)
std::cout << c[i] << " ";
}
#include <iostream>
using namespace std;
int main()
{
int aa[10][10];
for(int i = 0; i<10; i++)
for(int j = 0; j<10; j++)
aa[i][j] = i*10+j;
int col = 2;
// pointer to a length-10 1d array
int (*p)[10] = (int (*)[10])&(aa[0][col]);
for(int i =0; i<10; i++)
cout << *(p[i]) << endl;
return 0;
}
ARRAY ADDRESS AND POINTERS TO MULTIDIMENSIONAL ARRAYS 数组地址和指向多维数组的指针
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.