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

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

问题描述

我希望能够创建一个二维数组,其大小与我从文件中读取的宽度和高度相同,但是当我说:

int 数组[0][0]数组 = 新整数[宽度][高度]

解决方案

你应该使用指向指针的指针:

int** 数组;数组 = 新整数 * [宽度];for (int i = 0;i

当你用完或者想要调整大小时,你应该像这样释放分配的内存:

for (int i = 0;i

要理解并能够阅读更复杂的类型,此链接可能有用:

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天全站免登陆