简体   繁体   English

C ++中的2D int数组

[英]2D int array in C++

So I want to initialize an int 2d array very quickly, but I can't figure out how to do it. 所以我想非常快速地初始化一个int 2d数组,但是我不知道该怎么做。 I've done a few searches and none of them say how to initialize a 2D array, except to do: 我已经进行了几次搜索,除了执行以下操作之外,没有人说过如何初始化2D数组:

int [SOME_CONSTANT][ANOTHER_CONSTANT] = {{0}};

Basically, I've got 8 vertices, and I'm listing the 4 vertices of each face of a cube in an array. 基本上,我有8个顶点,并且我在数组中列出了多维数据集每个面的4个顶点。 I've tried this: 我已经试过了:

int[6][4] sides = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};

But that tells me that there's an error with 'sides', and that it expected a semi-colon. 但这告诉我,“两边”存在错误,并且期望使用分号。 Is there any way to initialize an array quickly like this? 有没有办法像这样快速初始化数组?

Thanks! 谢谢!

我想你是说

int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};

You have the [][] on the wrong side. 您将[] []放在错误的一侧。 Try this: 尝试这个:

int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};

Keep in mind that what you really have is: 请记住,您真正拥有的是:

int **sides

(A pointer to a pointer of ints). (指向整数的指针)。 It's sides that has the dimensions, not the int. 具有尺寸的是侧面,而不是int。 Therefore, you could also do: 因此,您也可以这样做:

int x, y[2], z[3][4], ...;

int array[n][m] behaves just like int array[n * m] . int array[n][m]行为就像int array[n * m]

In fact, array[i][j] = array[m * i + j] for all i, j . 实际上,对于所有i, jarray[i][j] = array[m * i + j]

So int array[2][3] = {1, 2, 3, 4, 5, 6}; 所以int array[2][3] = {1, 2, 3, 4, 5, 6}; is a valid declaration and, for example, 是有效的声明,例如

array[1][1] = array[3 * 1 + 1] = array[4] = 5 . array[1][1] = array[3 * 1 + 1] = array[4] = 5

int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}}; 

I'm not a regular c++ programmer but I looks like int sides[6][4] seems to compile while int[6][4] sides fails. 我不是一个普通的C ++程序员,但我看起来像int sides[6][4]看起来编译,而int[6][4] sides将失败。 Languages like C# lets you have the [][] on either sides but apparently c++ doesn't. 像C#这样的语言可以让您在两边都有[][] ,但显然c ++却没有。

int sides[6][4] = ... should do the trick. int sides[6][4] = ...应该可以解决问题。 This sounds like you may be coming from a Java (or other language) background so I do recommend a C++ book The Definitive C++ Book Guide and List for more details. 听起来您可能来自Java(或其他语言)背景,所以我推荐一本C ++书The Definitive C ++ Book Guide and List以获得更多详细信息。

Yes, the intended type of sides is int[6][4] , but C++ has confusing syntax sometimes. 是的,边的预期类型是int[6][4] ,但是C ++有时语法令人困惑。 The way to declare said array is: 声明所述数组的方式是:

int sides[6][4] = {/*stuff*/};

You run into this with function pointers too, but even worse: 您也遇到了函数指针,但是更糟糕的是:

int (*myfuncptr)(int); //creates a function pointer called myfuncptr

With function pointers though, you can do this: 使用函数指针,您可以执行以下操作:

typedef int (*func_ptr_type)(int);
func_ptr_type myfuncptr;

Unfortunately, there's no corresponding magic trick for arrays. 不幸的是,数组没有相应的魔术。

i would make a array outside of function and just assign it it to your local. 我会做一个函数之外的数组,只是将其分配给您的本地。 this will very likely invoke memcpy or just inline memory copying loop 这很可能会调用memcpy或只是内联内存复制循环

this is the fastest you can get 这是最快的速度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM