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

后端 未结 3 1656
情深已故
情深已故 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:52

    I shall answer with a simple analogy.

    What is "better" in general out of the two things?

    1. A telephone book where each entry is a code referring to a different book that you have to find and read to discover someone's telephone number
    2. A telephone book that lists people's telephone numbers

    Keeping all your data in a single big blob is more simple, more sensible, and easier on your computer's cache. A vector with N vectors inside it is much more operationally complex (remember, each of those requires a dynamic allocation and size management operations!); one vector is, well, one vector. You haven't multiplied the workload by N.

    The only downside really is that to simulate 2D array access with a 1D underlying data store, you need to write a facade. Fortunately, this is very easy.

    Now for the subjective part: on balance I'd say that it's worthwhile unless you're really in a rush and your code quality doesn't particularly matter.

提交回复
热议问题