在 C++ 中动态创建三维数组或二维数组

Dynamically create a Three-Dimensional array OR a Two-Dimensional Array in C++

我目前正在尝试制作一个程序,该程序将生成要导出到游戏中的迷宫。该程序将接受用户输入来设置迷宫的一些属性。如果迷宫只有两个维度(一层)或三个维度(两层或更多层),我想要的选项之一。为此,我在 Maze class 中动态分配一个数组,如下所示:

在Maze.hpp中:

class Maze {
    private:
        unsigned int width_, length_, height_;

        Cell*** matrix = nullptr;
};

在Maze.cpp中:

Maze::Maze() {           // Default constructor
    width_ = 20;
    length_ = 20;
    height_ = 0;

    matrix = new Cell**[width_];
    for (unsigned int x {}; x < width_; ++x) {
        matrix[x] = new Cell*[length_];
        for (unsigned int y {}; y < length_; ++y) {
            matrix[x][y] = new Cell(x, y);
        }
    }
}

Maze::Maze(int width, int length) {           // 2D maze constructor
    width_ = width;
    length_ = length;
    height_ = 0;

    matrix = new Cell**[width_];
    for (unsigned int x {}; x < width_; ++x) {
        matrix[x] = new Cell*[length_];
        for (unsigned int y {}; y < length_; ++y) {
            matrix[x][y] = new Cell(x, y);
        }
    }
}

Maze::Maze(int width, int length, int height) {    // 3D maze constructor
    width_ = width;
    length_ = length;
    height_ = height;

    matrix = new Cell**[width_];
    for (unsigned int x {}; x < width_; ++x) {
        matrix[x] = new Cell*[length_];
        for (unsigned int y {}; y < length_; ++y) {
            matrix[x][y] = new Cell[height];
            for (unsigned int z {}; z < height_; ++z) {
                matrix[x][y][z] = Cell(x, y, z);
            }
        }
    }
}

但如您所见,如果我使用二维,我最终会得到一个指向迷宫中每个单独单元格的指针,同时,如果我使用三个维度,我最终会得到一个单元格对象。如果在这两种情况下我都可以有一个单元格对象,我会更愿意,但我不知道如何实现它。

有办法吗?或者这是我唯一的选择?

如问,这里是Cell的声明:

Cell.hpp:

class Cell {
    private:
        unsigned int xPos_, yPos_, zPos_;
    public:
        Cell(unsigned int xPos, unsigned int yPos);
        Cell(unsigned int xPos, unsigned int yPos, unsigned int zPos);
        Cell();
};

Cell.cpp:

Cell::Cell(unsigned int xPos, unsigned int yPos) {
    xPos_ = xPos;
    yPos_ = yPos;
}

Cell::Cell(unsigned int xPos, unsigned int yPos, unsigned int zPos) {
    xPos_ = xPos;
    yPos_ = yPos;
    zPos_ = zPos;
}

根据问题评论中的建议,我将更改为 std::vector 而不是使用三重指针。

此外,由于 2D 数组只是一个在三维中只有一个值的 3D 数组,我将只更改代码。 (这在评论中也有建议)

完成后我会用新代码更新这个答案。

更新:

最终代码如下所示:

Cell.hpp:

class Cell {
    private:
        unsigned xPos_, yPos_, zPos_;
    public:
        Cell(unsigned xPos, unsigned yPos, unsigned zPos);
        Cell();
};

Cell.cpp:

Cell::Cell(unsigned xPos, unsigned yPos, unsigned zPos) {
    xPos_ = xPos;
    yPos_ = yPos;
    zPos_ = zPos;
}

Maze.hpp:

class Maze {
    private:
        unsigned width_, length_, height_;

        std::vector<std::vector<std::vector<Cell>>> matrix;

        void generateMatrix();
    public:
        Maze();
        Maze(unsigned width, unsigned length);
        Maze(unsigned width, unsigned length, unsigned height);
};

Maze.cpp:

Maze::Maze() {                                      // Default constructor
    width_ = 20;
    length_ = 20;
    height_ = 1;

    Maze::generateMatrix();
}

Maze::Maze(unsigned width, unsigned length) {                 // 2D maze constructor
    width_ = width;
    length_ = length;
    height_ = 1;

    Maze::generateMatrix();
}

Maze::Maze(unsigned width, unsigned length, unsigned height) {    // 3D maze constructor
    width_ = width;
    length_ = length;
    height_ = height;

    Maze::generateMatrix();
}

void Maze::generateMatrix() {
    for (unsigned x {}; x < width_; ++x) {
        matrix.push_back(std::vector<std::vector<Cell>>());

        for (unsigned y {}; y < length_; ++y) {
            matrix.at(x).push_back(std::vector<Cell>());

            for (unsigned z {}; z < height_; ++z) {
                matrix.at(x).at(y).push_back(Cell(x,y,z));
            }
        }
    }
}