Since:
Why would anyone prefer std::vector
to std::deque
?
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 .