C ++中的多维数组如何起作用? [英] How do multidimensional arrays in C++ function?

查看:61
本文介绍了C ++中的多维数组如何起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在C ++中使用数组?

简单地说,是C ++中的多维数组是数组的数组,还是仅仅是行为类似于多维数组的单个数组?

我的一位朋友解释说,C ++中的数组只是常量指针,而多维数组也是常量指针,其元素由多个索引寻址(即,它们是指向一个大数组的指针,且其偏移量为元素是使用多个索引来计算的.)

我相信多维数组是指向其他数组的指针的一维数组,因为当将多维数组作为函数参数传递时,我经常使用以下语法:

  void copy_matrix(int ** matrix); 

如果没有,是否有可能在C ++中创建一个数组数组并在编译时分配每个子数组的值-等同于以下语句的语义:

  int行[2] [2];line [0] = {100,100};line [1] = {200,200}; 

以上语句生成一个编译时错误.我想到的一个快速(未经测试)的技巧是:

  int行[2] [2];void Assign(int index,int *点,int length){for(int i = 0; i< length; i ++){line [index] [i] = point [i];}}分配(0,{100,100},2);Assign(1,{200,200},2); 

但是,我相信必须有一种更优雅的方法来达到相同的结果.我希望能清楚地理解这些概念,并感谢您的投入.

解决方案

大括号列表初始化程序仅在初始化期间起作用,并且您似乎无法尝试将其用于赋值.

相反,只需一次初始化多维数组:

  int行[2] [2] = {{100,100},{200,200}}; 

回应您的第一句话:数组是数组,指针是指针,它们不是一回事.但是,数组确实容易衰减到指向第一个元素的指针,并且 x [n] 等效于 *(x + n).这同样适用于多维数组.

数组 T [N] 在内存中是连续的,用于 T sizeof(T [N])== N * sizeof(T);因此, T [N] [M] 的数组对于 T [N] T 都是连续的.

Possible Duplicate:
How do I use arrays in C++?

To put it simply, is a multidimensional array in C++ an array of arrays or is it simply a single array which behaves like it's multidimensional?

A friend of mine explained that arrays in C++ are simply constant pointers, and that multidimensional arrays are also constant pointers whose elements are addressed by more than one index (i.e. they are pointers pointing to one big array, and the offset of the elements are calculated using multiple indices).

I believe that multidimensional arrays are single dimensional arrays of pointers which point to other arrays because when passing multidimensional arrays as function arguments I often use the syntax:

void copy_matrix(int ** matrix);

If not, is it possible to create an array of arrays in C++ and assign the value of each sub-array at compile time - equivalent to the semantics of the following statements:

int line[2][2];

line[0] = {100, 100};
line[1] = {200, 200};

The above statements generate a compile time error. A quick (untested) hack I came up with was:

int line[2][2];

void assign(int index, int * point, int length) {
    for (int i = 0; i < length; i++) {
        line[index][i] = point[i];
    }
}

assign(0, {100, 100}, 2);
assign(1, {200, 200}, 2);

However, I believe that there must be a more elegant way to achieve the same result. I hope to clearly understand these concepts, and any input is appreciated.

解决方案

The brace-list initializer only works during initialization, and it cannot be used for assignment, as you seem to attempt.

Instead, just initialize the multi-dimensional array all at once:

int line[2][2] = { {100, 100}, {200, 200} };

In response to your first sentence: Arrays are arrays and pointers are pointers, and they're not the same thing. An array does however decay to a pointer to the first element with ease, and x[n] is equivalent to *(x + n). This applies recursively to multi-dimensional arrays as well.

Arrays T[N] are contiguous in memory for T and sizeof(T[N]) == N * sizeof(T); therefore, arrays of arrays T[N][M] are contiguous both for T[N] and for T.

这篇关于C ++中的多维数组如何起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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