std::vector of std::vectors contiguity

后端 未结 4 1625
暗喜
暗喜 2020-12-01 15:56

I know that std::vector internally stores it\'s data contiguously (unless it is std::vector) both in the old C++03

4条回答
  •  悲&欢浪女
    2020-12-01 16:22

    is it possible to access all the elements stored in such nested structure "simply" and sequentially (via a pointer or similar) the same way it can be done for a 1-D vector?

    Yes, if:

    • you only ever need to add stuff to the end of your vector of vectors, and

    • you're willing to replace the vector of vectors construct with a custom data structure

    What you can do then is to concatenate all those sub vectors into a single contiguous buffer, with another index buffer used for accessing this by top level entry index.

    See my article here for more discussion about this, and an example 'collapsed vector vector' class implementation..

提交回复
热议问题