I know that deque is more efficient than vector when insertions are at front or end and vector is better if we have to do pointer arithmetic. But which one to use when we have to perform insertions in middle.? and Why.?
You might think that a deque
would have the advantage, because it stores the data broken up into blocks. However to implement operator[]
in constant time requires all those blocks to be the same size. Inserting or deleting an element in the middle still requires shifting all the values on one side or the other, same as a vector
. Since the vector
is simpler and has better locality for caching, it should come out ahead.