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

后端 未结 3 1657
情深已故
情深已故 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 23:10

    Using a vector of vectors:

    1. Is inefficient in terms of memory allocation, due to multiple blocks being allocated.
    2. Models a jagged right hand edge, so bugs can creep in.

    Using a single vector is, in general, better as the memory management is simpler. But you can encounter problems if your matrix is large as it can be difficult to acquire a large contiguous block.

    If your array is resizeable, then I'd still stick to a single vector: the resize complexity can be isolated in a single function that you can optimise.

    The best solution of all is, of course, to use something like the linear algebra library (BLAS), available in Boost. That also handles large sparse matrices beautifully.

提交回复
热议问题