我有一个家庭作业。我不是在寻找任何人为我做这项工作,我只是在一个小方面遇到了麻烦,尽管我也会接受其他方面的建议。
任务是:
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.
- 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.
- 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.
- 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/