English 中文(简体)
采用双向动态阵列的写作班
原标题:Write a class using a two-dimensional dynamic array
  • 时间:2009-10-07 19:31:45
  •  标签:

我有家务劳动。 我不想让任何人为我做工作,但我只想稍有麻烦,尽管我接受关于其他界限的建议。

任务是:

采用两维动力阵列撰写一个班级。

施工人穿透阵列的各个方面。 施工人员还将动态阵列中的所有数值与增长指数乘以一栏指数。

  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.

我需要帮助了解如何在私营部门宣布2D阵列。 并且,如上所述,如果任何人能够给我以其他内容,说明如何做到这一点,将不胜感激。

最佳回答

这是自我完成C++发展以来的,但如果我记得正确的话,你做的就是:

int rows = 5;
int cols = 10;

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

我可能错了;我看到在网上出现相反的情况,即你必须用单一尺寸的2D阵列,并改变你的说明:

rel=“noreferer” http://en.allexperts.com/q/C-1040/creating-2D-array-dynamically.htm

问题回答

这应当给你一个想法。 我不想编纂这一法典:

class Array {
   int **arr;
};

Array::Array(int rows, int cols) {
    arr = new int * [rows];  // this will allocate  rows  many  int * s
    if (arr) {               // to ensure memory was allocated
        for (int i = 0; i < rows; i++) {
            arr[i] = new int [cols];  // this will allocate  cols  many  int s
            assert(arr[i]);           // to ensure memory was allocated
        }
    }
}

<代码>arr是一系列点码的点码。 arr[i] is a pointer to a range of ints i.e. each arr[i] represent onerow in their 2-D range. 这应该让你们开始。

如果你真的想学习C++,尝试事情,学习如何使用夸张。 如果你对夸张感到安慰,则使用印刷品来查明哪些情况是错误的。

class TwoDimensionalArray {
  private:
    int **array;

  public:
    TwoDimensionalArray(const int, const int);
};

TwoDimensionalArray::TwoDimensionalArray(const int rows, const int columns) {
    array = new int *[rows];
    for (int i = 0; i < rows; i++)
        array[i] = new int[columns];
}

int main() {
    TwoDimensionalArray *arr1 = new TwoDimensionalArray(5, 10);
    return 0;
}




相关问题
热门标签