In C++, you don't need C-style typedef struct {...} ...
: you can just define a struct
without typedef
, like this:
struct Cell {
Point CellLocation;
enumCell isOccupied;
....
};
Moreover, instead of using raw C-style arrays allocated with new[]
(and manually freed with delete[]
, and exception-unsafe), you can use C++ convenient container classes, like a
std::vector
containing another std::vector
, e.g.:
// 2D matrix of cells (as vector of vector)
vector<vector<Cell>> cells;
// Create the rows
for (size_t i = 0; i < Height; i++) {
// Add an empty row
cells.push_back(vector<Cell>());
}
// Add columns to each row
for (size_t j = 0; j < Width; j++) {
for (size_t i = 0; i < cells.size(); i++) {
cells[i].push_back(Cell());
}
}
And you can use the cells[i][j]
syntax to access single elements in the matrix.
An alternative would be to use a single vector
, simulating a 2D matrix:
// 2D matrix, simulated using a 1D vector
vector<Cell> cells(Height * Width);
// Access element at index (row, column), using:
cells[row * Width + column] = ... ;