二维阵列VS的阵列的结构 [英] 2d array vs structure of an array

查看:123
本文介绍了二维阵列VS的阵列的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C语言,所以我的问题可能不符合高标准

I am new to C language, so my question may not meet high standards

我们可以使用

struct mat{

  int a[10];

};m[10];

而不是

int mat[10][10];

什么区别?
并且这是更有效的

whats the difference? and which is more efficient?

推荐答案

您应该信任与编译器。不过,我可以建议2的替代品,我知道的作品,这具体取决于您的应用程序。

You should trust the compiler with that. However, I may suggest 2 alternatives that I know works well depending on your application.

1)使用int类型的[10] [10],但要确保循环,当你访问他们在正确的顺序。编译器将下面使用一个单一的阵列结构使acessing为了

1) use int a[10][10] but make sure you access them in the proper order when looping. The compiler will "underneath" use a single array structure so acessing in order

  for( i = 0 ; i < 10 ; i++) {
     for (j = 0 ; j < 10 ; j++) {
         // do something with a[i][j]
     }
  }

VS

  for( i = 0 ; i < 10 ; i++) {
     for (j = 0 ; j < 10 ; j++) {
         // do something with a[j][i]
     }
  }

是在性能方面不同。后者是更好的性能。

is different in terms of performance. The later is more performant.

2)选项1需要格外小心,是反直觉。我更preFER做

2) The option 1 requires extra care and is counter-intuitive. I much prefer to do

   int a[100]

和做

   for( i = 0 ; i < 100 ; i++)
      inline_function(a[i]);

其中函数应该内联声明和preform你有事情做了。如果你能避免的功能,它甚至更好。例如,如果它是一个总和,有它的2D或矢量不会改变任何东西。

where the function should be declared inline and preform the thing you have to have done. If you can avoid the function, it's even better. For example, if it's a sum, having it 2d or vector doesn't change anything.

编辑:这是在细节解释的引用有关阵列顺序位: HTTP ://www.cplusplus.com/doc/tutorial/arrays/

here is a reference that explain in the details the bit about the array order: http://www.cplusplus.com/doc/tutorial/arrays/

这篇关于二维阵列VS的阵列的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆