I'd like to design a crossword puzzle editor in C++. It is a grid of blocks, each block containing a letter (or being black between two words), possibly a number and a thick or thin border line. The block is therefore a container class for them. The grid is a container of blocks. But how would I structure the grid?
- A raw 2d array:
Block grid[row][column]? - Vector of Vectors:
vector<vector<Block>>? - Two vectors, one for the rows and one for the columns:
vector<Block> row; vector<Block> column? - A map, which keys are the row/column pairs and the values are the blocks:
map<int[2], Block>?
By default, plain static/dynamic arrays (or their wrappers) are the most preferable: they are the most comfortable for both the programmer (random access API etc) and the processor (memory locality etc).
The easiest-to-implement
Blocklayout in an array/a vector is[first row Blocks..., second row Blocks..., etc]- a 1D array which acts as a 2D array. It can be indexed likecrossword[x + y * crossword.width()], which isn't pretty, so you might want to use a library/self-written wrapper with API likecrossword(x, y)which performs thatxy-to-i-index conversion under the hood.Maybe something like this: