c++ - 使用二维动态数组写一个类

标签 c++ arrays dynamic class

我有一个家庭作业。我不是在寻找任何人为我做这项工作,我只是在一个小方面遇到了麻烦,尽管我也会接受其他方面的建议。

任务是:

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. Swap two columns of the two-dimensional array, where the column indexes are passed in as parameters. Do this just by copying addresses, not values of column elemnets.
  2. Delete a column of the two-dimensional array, where the column index is passed in as a parameter. Do not just use the delete operator on the column array and set the horizontal array element to NULL. Shrink the size of the horizontal array by 1.
  3. Create a print function for the class to print out the values of the two-dimensional array and make sure that your functions are working correctly. After you know that they are working correctly, delete the print function.

我需要帮助了解如何在私有(private)部分中声明二维数组。而且,如前所述,如果有人能给我其他关于如何做的提示,我将不胜感激。

最佳答案

自从我完成 C++ 开发以来已经有一段时间了,但如果我没记错的话,你会做如下的事情:

int rows = 5;
int cols = 10;

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

我可能错了;我在网上看到相反的帖子,你必须用一维数组伪造二维数组并更改你的下标:

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

关于c++ - 使用二维动态数组写一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1533687/

相关文章:

c++ - 如何安装 SFGUI 和 SFML visual Studio

c++ - 截取特定窗口的屏幕截图 - C++/Qt

C++ 优雅地预测大多数字符串数组

无法在c中传递正确的动态结构数组

java - android中listview的动态定位

javascript - 动态元素上的 Jquery 选择器

c++ - 如何释放指针 vector ?

C++ 在运行时崩溃,但只是在某些时候

c# - 如何查看 C# 数组在内存中的放置方式?

python - 如何在 Python Pandas 中的数组、列表或数据帧中的每个单个数字元素的末尾 append 1?