std::vector and contiguous memory of multidimensional arrays

后端 未结 6 1814
青春惊慌失措
青春惊慌失措 2020-11-27 07:42

I know that the standard does not force std::vector to allocate contiguous memory blocks, but all implementations obey this nevertheless.

Suppo

6条回答
  •  Happy的楠姐
    2020-11-27 07:59

    The standard does require the memory of an std::vector to be contiguous. On the other hand, if you write something like:

    std::vector > v;
    

    the global memory (all of the v[i][j]) will not be contiguous. The usual way of creating 2D arrays is to use a single

    std::vector v;
    

    and calculate the indexes, exactly as you suggest doing with float. (You can also create a second std::vector with the addresses if you want. I've always just recalculated the indexes, however.)

提交回复
热议问题