C++ 中的二维整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7665574/
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
2D int array in C++
提问by Casey Kuball
So I want to initialize an int 2d array very quickly, but I can't figure out how to do it. I've done a few searches and none of them say how to initialize a 2D array, except to do:
所以我想非常快速地初始化一个 int 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. I've tried this:
基本上,我有 8 个顶点,我在数组中列出了立方体每个面的 4 个顶点。我试过这个:
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!
谢谢!
回答by minus
I think You meant to say
我想你的意思是说
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}};
回答by Derek Springer
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. Therefore, you could also do:
(指向整数指针的指针)。有尺寸的是边,而不是整数。因此,您还可以这样做:
int x, y[2], z[3][4], ...;
回答by Dennis
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
.
事实上,array[i][j] = array[m * i + j]
对于所有i, j
.
So int array[2][3] = {1, 2, 3, 4, 5, 6};
is a valid declaration and, for example,
所以int array[2][3] = {1, 2, 3, 4, 5, 6};
是一个有效的声明,例如,
array[1][1] = array[3 * 1 + 1] = array[4] = 5
.
array[1][1] = array[3 * 1 + 1] = array[4] = 5
.
回答by Bala R
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. Languages like C# lets you have the [][]
on either sides but apparently c++ doesn't.
我不是一个普通的 C++ 程序员,但我看起来int sides[6][4]
似乎在编译时int[6][4] sides
失败了。像 C# 这样的语言可以让你[][]
在两边都有,但显然 c++ 没有。
回答by Mark B
int sides[6][4] = ...
should do the trick. 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 Listfor more details.
int sides[6][4] = ...
应该做的伎俩。听起来您可能具有 Java(或其他语言)背景,因此我推荐一本 C++ 书籍The Definitive C++ Book Guide and List了解更多详细信息。
回答by Mooing Duck
Yes, the intended type of sides is int[6][4]
, but C++ has confusing syntax sometimes. The way to declare said array is:
是的,预期的边类型是int[6][4]
,但 C++ 有时会出现令人困惑的语法。声明所述数组的方法是:
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.
不幸的是,数组没有相应的魔术技巧。
回答by fazo
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
这是你能得到的最快速度