What are the Issues with a vector-of-vectors?

后端 未结 3 1654
情深已故
情深已故 2020-11-27 22:32

I\'ve read that a vector-of-vectors is bad given a fixed 2nd dimension, but I cannot find a clear explanation of the problems on http://

3条回答
  •  [愿得一人]
    2020-11-27 22:57

    For a std::vector the underlying array is dynamically allocated from the heap. If you have e.g. std::vector>, then your outer vector would look like

    {v1, v2, v3, v4, ... vn}
    

    This looks like each of the inner vectors will be in contiguous memory, and they will, but their underlying arrays will not be contiguous. See the diagram of the memory layout in this post. In other words the you cannot say that

    &(v1.back()) + 1 == &(v2.front()) // not necessarily true!
    

    Instead if you used a single vector with striding then you would gain data locality, and it would inherently be more cache friendly as all your data is contiguous.

    For the sake of completeness, I would use neither of these methods if your matrix was sparse, as there are more elegant and efficient storage schemes than straight 1D or 2D arrays. Though since you mentioned you have a "fixed 2nd dimension" I will assume that is not the case here.

提交回复
热议问题