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

问题描述 投票:0回答:1

我目前正在尝试制作一个程序,该程序会生成迷宫并将其导出到游戏中。该程序将接受用户输入以设置迷宫的某些属性。我希望迷宫只有两个尺寸(一个楼层)或三个尺寸(两个或两个以上楼层)时可以选择一种。为此,我在Maze类中动态分配了一个数组,如下所示:

在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.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;
}
c++ arrays multidimensional-array dynamic-memory-allocation dynamic-arrays
1个回答
0
投票

如问题注释中所建议,我将改为std::vector而不是使用三重指针。

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

完成后,我将使用新代码更新此答案。

UPDATE:

这是最终代码的样子:

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));
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.