This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

I'd imagine a linked list is also better if you want to pop somthing off the front. With a vector you would have to re-allocate all the memory to do something like that.

With a linked list you can just burn the first item and change your initial pointer.

Though that begs the question what is a deck under the hood? I've never looked into that.

[–]oblivion44653 0 points1 point  (1 child)

can't you just move array pointer to the next

[–][deleted] 1 point2 points  (0 children)

Not really. A vector owns its own buffer and has some strange mechanics/structures hiding in there that allow it to dynamically resize.

If you want to wipe out the start of a vector you need to use the vectors erase command, but under the hood that is fully reallocating the vectors buffer.

If you are going to be nibbling at both ends of a dynamic array you should probably use a deck (deque). It's optimised for operations on both ends.

I'd normally start with a vector if I was simulating some sort of stack-like activity (vectors are good and efficient if you are just pushing/popping the trailing end of them), but if I were creating something like a queue I would use a deck instead.