使用二维动态数组编写类 [英] Write a class using a two-dimensional dynamic array

查看:156
本文介绍了使用二维动态数组编写类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份家庭作业。我不想为任何人做我的工作,我只是有一个小方面的麻烦,虽然我也接受其他位的建议。

I have a homework assignment. I'm not looking for anyone to do the work for me, I'm just having trouble with one little aspect, although I'd accept advice on other bits as well.

赋值是:


使用二维动态数组写一个类。

Write a class using a two-dimensional dynamic array.

构造函数传递数组的维度。

The constructor passes in the dimensions of the array. The constructor also intializes all values in the dynamic array to the row index multiplied by the column index.


  1. 交换两列的二维数据数组,其中列索引作为参数传递。只需复制地址,而不是列elemnets的值即可。

  2. 删除二维数组的列,其中列索引作为参数传入。不要只对列数组使用delete操作符,并将水平数组元素设置为NULL。缩小水平数组的大小为1。

  3. 为类打印出打印函数,打印出二维数组的值,并确保函数正确工作。在您知道它们正常工作后,请删除打印功能。


我需要帮助了解如何在私有节中声明2D数组。

I need help understanding how to declare the 2D array in the private section. And, as mentioned, if anyone could give me other hints on how to do it, that would be appreciated.

推荐答案

这是已经被提及的自从我做了C ++开发,但是如果我记得正确,你会做一些类似如下:

It's been awhile since I've done C++ development, but if I remember correctly, you'd do something like th efollowing:

int rows = 5;
int cols = 10;

int** array = new int*[rows];
for (int i = 0; i < rows; i++) {
     array[i] = new int[cols];
}

我看到相反的帖子在线,你必须伪造一个二维数组的二维数组,并改变下标:

I might be mistaken; I see posts to the contrary online where you have to fake 2D arrays with a single dimensional array and change your subscripting:

http://en.allexperts.com/q/C-1040/creating-2D-array-dynamically .htm

这篇关于使用二维动态数组编写类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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