【问题标题】:c++ how do you convert 2D Array to 2D array using Double pointers -- m_array : Array<T> to m_array T**c++ 如何使用双指针将 2D 数组转换为 2D 数组 -- m_array : Array<T> to m_array T**
【发布时间】:2018-07-05 07:21:34
【问题描述】:

我正在尝试使用我的 2D 数组程序并将类私有设置从 (UML CODE) -m_array: Array&lt;T&gt; 更改为 -m_array : T**。这是数据结构类的要求......其中一项作业

Array<TYPE> m_array;    // m_array : Array<TYPE> 

TYPE ** m_array;        // m_array : T** 

Array2D.h

template <class TYPE>
class Array2D
{
public:
    Array2D();
    Array2D(int row, int col);
    Array2D(const Array2D<TYPE> & copy);
    ~Array2D() {}

    const Array2D<TYPE> & operator=(const Array2D<TYPE>& rhs);
    const Row<TYPE> operator[](int index) const;
    Row<TYPE> operator[](int index);
    const int getRow() const { return m_rows; }
    void setRow(int rows);
    const int getColumn() const { return m_cols; }
    void setColumn(int columns);
    TYPE & Select(int row, int col);

private:
    Array<TYPE> m_array;  // This needs to change
    int m_rows;
    int m_cols;

    bool ValidCol(int column) { return (column < m_cols || column >= 0);}
    bool ValidRow(int row) { return (row < m_rows || row >= 0); }
};

如果我将私有 Array&lt;TYPE&gt; m_array 更改为 TYPE ** m_array;,我会遇到一些 Array2D 函数的问题。不知道如何更改我的代码以使用双指针。

template<class TYPE>
Array2D<TYPE>::Array2D(int row, int col) : m_rows(row), m_cols(col)
{
    cout << "User specified 2D constructor " << endl;
    cout << "Row = " << row << " and Col = " << col << "... R*C = " << (row * col) << endl << endl;

    if (m_rows < 1 || m_cols < 1)
        throw Exception("Invalid dimensions");
    else
        m_array = Array<TYPE>(m_rows*m_cols);   // <--- Issues
}

我收到类似的错误

...错误 C2440:'=':无法从 'Array' 转换为 'int **'

如果我不考虑上面的例子。

数组.h

template <class TYPE>
class Array
{
public:
    Array();
    Array(int length, int beg_index = 0);
    Array(const Array<TYPE>& copy);
    ~Array();

    const Array<TYPE> & operator=(const Array<TYPE>& rhs);    
    TYPE & operator[](int index);
    const TYPE & operator[](int index) const;
    int  getLength() const { return m_length; }
    inline void setLength(int new_length);
    int  getStartIndex() const {return m_start_index; }
    void setStartIndex(const int new_start_index) {m_start_index = new_start_index;}

private:
    bool WithinBounds(const int loc_request) const;
    bool ValidLength(const int requested_length)const;

    TYPE * m_array = 0;
    int m_length;
    int m_start_index;
};

您可以提出任何建议,我们将不胜感激。我从来没有完全理解指针,但有一个基本的掌握。

【问题讨论】:

  • 为什么要TYPE **?这太糟糕了。 Array 怎么了?
  • T** 不是矩阵的正确数据类型,矩阵不是列表列表。
  • 数据结构类的要求是采用二维数组并使用双指针...如果需要我可以发布整个代码。

标签: c++ arrays double-pointer


【解决方案1】:

使用指针来分配矩阵的典型方法是:

int **M = new int*[numRows];

for (i = 0; i < numRows; i++) {
    M[i] = new int[numCols];
}

首先分配一个整数指针数组,然后为每个指针分配一个整数数组。然后您可以执行以下操作:

for (i = 0; i < numRows; i++) {
    for (j = 0; j < numCols; j++ ) {
        M[i][j] = (10 * i) + j;
    }
}

for (i = 0; i < numRows; i++) {
    for (j = 0; j < numCols; j++) {
        std::cout << M[i][j] << " ";
    }
    std::cout << std::endl;
}

【讨论】:

【解决方案2】:

如果浅拷贝就足够了,您可以使用calloc( sizeof(TYPE*), a.getLength() ); 创建一个数组,然后在循环内将每个元素初始化为&amp;a[i][0]。 (或addressof。)如果必须进行深拷贝,请使用new[] 创建矩阵的每一行并使用memcpy()

或者,您可以使用std::vector 完成所有操作,当您需要转换为C 样式指针和&lt;algorithm&gt; 库来操作它们时,只需调用.data()

当您确实要选择自己的数据结构时,我会表示这不是一个好的数据结构。

【讨论】:

    猜你喜欢
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多