vector a deque or a list?

closed account (z0My6Up4)
This is an exercise from C++ Primer that I was not sure about:

"Which is the most appropriate - a vector, a deque or a list. Explain the rationale for your choice. If there is no reason to prefer one or another container explain why not"

"..read an unknown number of words. Always insert new words at the back. Remove the next value from the front".

I think this could be done by either a vector or deque. I have ruled out using a list. A deque allows fast random access to add elements to either end of the container. But it is not clear to me whether removing an element at the front of a deque would be more efficient than trying it on a vector.

A vector allows efficient inserting of new elements at the back but how efficient is it to remove an element from the front?

Which would you choose and why?
i think it would be dequeue, since iirc it was designed for that
The deque will perform marginally better because a vector attempts to keep it's elements contiguous so when when it needs to expand it has to copy itself in it's entirety to a new memory page. The deque will happily link across non continuous blocks of memory.
closed account (z0My6Up4)
Ok thanks.. but I found out that a vector does not necessarily have to copy itself to new memory when it expands, because it's capacity may have already allocated enough memory to accommodate new elements. It's recommended to use a vector unless there is a good reason to use another container, so I would lean towards a vector for this reason
Last edited on
Topic archived. No new replies allowed.