C++ Two Dimensional std::vector best practices

后端 未结 4 983
栀梦
栀梦 2021-02-13 14:38

I am building an app that needs to have support for two dimensional arrays to hold a grid of data. I have a class Map that contains a 2d grid of data. I want to use

4条回答
  •  再見小時候
    2021-02-13 15:05

    Use a vector and translate the 2 dimensions to one dimension. E.g. if your matrix has a width of W and a height of H, then mapping x,y to the index in the vector is simply x*W+y.

    If your matrix is sparse you may want to use an std::map where the key is a pair (x and y).

提交回复
热议问题