创建一个二维数组具有可变大小的尺寸 [英] Create a 2D array with variable sized dimensions

查看:114
本文介绍了创建一个二维数组具有可变大小的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个二维数组的宽度和高度,我从文件中读取的大小,但我得到的错误,当我说:

  int数组[0] [0]
阵列=新的INT [宽度] [高]


解决方案

您应该使用指针的指针:

  INT **阵列;
阵列=新INT * [宽度]
的for(int i = 0; I<宽度;我++)
    数组[我] =新的INT [身高];

和当你完成使用它,或者你要调整,你应该释放这样分配的内存:

 的for(int i = 0; I<宽度;我++)
    删除[]数组[我]
删除[]数组;

要了解并能够读取更复杂的类型,这个环节可能是有用的:

<一个href=\"http://www.unixwiz.net/techtips/reading-cdecl.html\">http://www.unixwiz.net/techtips/reading-cdecl.html

希望这是有帮助的。

I want to be able to create a 2d array the size of the width and height I read from a file, but I get errors when I say:

int array[0][0]
array = new int[width][height]

解决方案

You should use pointer to pointers :

int** array;
array = new int*[width];
for (int i = 0;i<width;i++)
    array[i] = new int[height];

and when you finish using it or you want to resize, you should free the allocated memory like this :

for (int i = 0;i<width;i++)
    delete[] array[i];
delete[] array;

To understand and be able to read more complex types, this link may be useful :

http://www.unixwiz.net/techtips/reading-cdecl.html

Hope that's Helpful.

这篇关于创建一个二维数组具有可变大小的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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