Why would I prefer using vector to deque

2019-01-10 05:53发布

Since

  1. they are both contiguous memory containers;
  2. feature wise, deque has almost everything vector has but more, since it is more efficient to insert in the front.

Why whould anyone prefer std::vector to std::deque?

9条回答
淡お忘
2楼-- · 2019-01-10 06:33

I've implemented both vector and deque multiple times. deque is hugely more complicated from an implementation point of view. This complication translates to more code and more complex code. So you'll typically see a code size hit when you choose deque over vector. You may also experience a small speed hit if your code uses only the things the vector excels at (i.e. push_back).

If you need a double ended queue, deque is the clear winner. But if you're doing most of your inserts and erases at the back, vector is going to be the clear winner. When you're unsure, declare your container with a typedef (so it is easy to switch back and forth), and measure.

查看更多
你好瞎i
3楼-- · 2019-01-10 06:35

std::deque doesn't have guaranteed continuous memory - and it's often somewhat slower for indexed access. A deque is typically implemented as a "list of vector".

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-10 06:41

I think that good idea to make perfomance test of each case. And make decision relying on this tests.

I'd prefer std::deque than std::vector in most cases.

查看更多
登录 后发表回答