Why would I prefer using vector to deque

c++, deque, stl, vector

Solution

Elements in a `deque` are not contiguous in memory; `vector` elements are guaranteed to be. So if you need to interact with a plain C library that needs contiguous arrays, or if you care (a lot) about spatial locality, then you might prefer `vector`. In addition, since there is some extra bookkeeping, other ops are probably (slightly) more expensive than their equivalent `vector` operations. On the other hand, using many/large instances of `vector` may lead to unnecessary heap fragmentation (slowing down calls to `new`).

Also, as pointed out elsewhere on StackOverflow, there is more good discussion here: http://www.gotw.ca/gotw/054.htm .

Problem

Since - they are both contiguous memory containers; - 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`?

Original source

Related problems