taby said:
I have game pieces, and I store them in a list, because they get erased all the time. In a list, the erasure costs a constant amount of time — O(1) time complexity. When you do an erasure in the middle of a vector, the time complexity is like O(n/2)… much larger, where n is the number of elements in the vector. There is a reason why both containers exist in the STL. ?
But iterating the list means chasing pointers, so it's very slow because incoherent access.
yeah… we could discuss pro and con of various data structures til the end of time. : )
But it shows the problem is not really how to allocate memory, but how to address it.
In general we want those things:
Resolving indirection quickly.
Iterate in linear memory order for most.
Reduce dynamic allocation and fragmentation.
All contradict each other, so its hard to propose general solutions that work best for everything.