我有一个由单元格组成的2D网格类,我必须将这些单元格存储在对2D网格进行建模的1D std::vector
(根据规格)中。
我已经编写了一个Grid::resize
函数,该函数将网格的大小调整为新的宽度和高度,但保留了保留区域内的网格内容(所有新单元格都设置为Cell::DEAD
)。
[我编写了一个调整大小的函数,该函数在运行时会导致分段错误:11,我认为这意味着该函数正在尝试访问向量边界之外的数据,但我不确定错误在哪里。
班级
class Grid {
private:
std::vector<Cell> grid_cells;
unsigned int width;
unsigned int height;
public:
Grid(const unsigned int width, const unsigned int height);
}
构造函数代码
Grid::Grid(const unsigned int width, const unsigned int height) {
std::vector<Cell> cells(width*height);
int i;
for(i=0;i<(width*height);i++){
cells[i] = Cell::DEAD;
}
this->width = width;
this->height = height;
this->grid_cells = cells;
}
调整大小功能
void Grid::resize(const unsigned int new_width, const unsigned int new_height) {
std::vector<Cell> new_cells(new_width*new_height);
unsigned int x, y;
for(x = 0; x < new_width; x++) {
for(y = 0; y < new_height; y++) {
if(x < this->width && y < this->height) {
new_cells[get_index(x,y)] = this->grid_cells[get_index(x,y)];
}
else {
new_cells[get_index(x,y)] = Cell::DEAD;
}
}
}
this->width = new_width;
this->height = new_height;
this->grid_cells = new_cells;
}
我的代码正在通过在其上运行预制测试套件进行测试,我相信该错误来自for循环的主体,因为当我将其注释掉时,测试套件将运行到最后,但是当它离开时,我得到一个细分错误,它停止运行。
编辑:
[get_index
函数,用于将2D x,y坐标转换为1D索引:
const unsigned int Grid::get_index(const unsigned int x, const unsigned int y) const{
return ((y * this->width) + x + 1);
}
const unsigned int
Grid::get_index(const unsigned int x, const unsigned int y) const{
return ((y * this->width) + x + 1);
}
您正在使用原始网格宽度来计算新网格中的索引。它们可能有很大的不同,因此您将复制错误的单元格或访问new_cells
此外,实际上,当您尝试访问坐标为(width-1, height-1)
的最后一个单元格(位于height*width - width + width -1 + 1 = height*width
时,总是执行无效的内存访问)。向量的最后一个有效索引是height*width-1
您可以使用免费功能
unsigned int
get_index(const unsigned int x, const unsigned int y, const unsigned int width) const{
return (y * width) + x ;
}
并且有
new_cells[get_index(x,y, new_width)] = this->grid_cells[get_index(x,y, this->width)]