The default data structure that you need to think about in C ++ is Vector .
Consider the following points ...
1] Bypass:
List nodes are scattered throughout the memory, and therefore traversing the list leads to misses in the cache . But vector traversal is smooth.
2] Insert and delete:
The average 50% of the elements should be shifted when you do this with Vector, but the caches are very good at that! But, with lists, you need to go to the insert / delete point ... so again ... cache misses! And surprisingly, vectors also win this case!
3] Storage:
When you use lists, there are two pointers to elements (forward and backward), so the list is much larger than the vector! Vectors require a bit more memory than actual elements need.
Yout must have a reason not to use a vector.
Reference:
I found out about this in a conversation with Lord Bjarn Straustup:
https://youtu.be/0iWb_qi2-uI?t=2680Sam Jan 25 '13 at 16:45 2013-01-25 16:45
source share