Search code examples
c++containers

Linked-list based vector


I was thinking about the downside of using the std::vector container, and was wondering if using a chunked linked-list as a back-end might avoid the copy that occurs when the vector extends.

Something like this.

I guess my question is, is this a practical idea and am I right to assume that reading from the container would have similar runtime as the array-based vector, while the "grow" time would be vastly reduced?


Solution

  • The run-time (random) access of your solution would be greater than that of the std::vector.

    In order to access element N, you may have to go through many links to get to the appropriate block, then access an element through the block.

    The performance of large vectors can be reduced by allocating a larger size up front.

    If insert and removal is frequent, perhaps a vector is the wrong data structure.