Why would I prefer using vector to deque

前端 未结 10 1719
陌清茗
陌清茗 2020-11-29 17:10

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
相关标签:
10条回答
  • 2020-11-29 18:05

    A deque is a sequence container which allows random access to it's elements but it is not guaranteed to have contiguous storage.

    0 讨论(0)
  • 2020-11-29 18:06

    You woudn't prefer vector to deque acording to these test results (with source).

    Of course, you should test in your app/environment, but in summary:

    • push_back is basically the same for all
    • insert, erase in deque are much faster than list and marginally faster than vector

    Some more musings, and a note to consider circular_buffer.

    0 讨论(0)
  • 2020-11-29 18:06

    On the one hand, vector is quite frequently just plain faster than deque. If you don't actually need all of the features of deque, use a vector.

    On the other hand, sometimes you do need features which vector does not give you, in which case you must use a deque. For example, I challenge anyone to attempt to rewrite this code, without using a deque, and without enormously altering the algorithm.

    0 讨论(0)
  • 2020-11-29 18:15

    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".

    0 讨论(0)
提交回复
热议问题