c++ - deque vs vector guidance after C++11 enhancements -
this question has answer here:
back in pre-c++11 days, many book authors recommended use of deque
situations called dynamically sized container random access. in part due fact deque
move versatile data structure vector
, due fact vector
in pre-c++11 world did not offer convenient way size down capacity via "shrink fit." greater deque
overhead of indirect access elements via brackets operator , iterators seemed subsumed greater vector
overhead of reallocation.
on other hand, things haven't changed. vector
still uses geometric (i.e., size*factor) scheme reallocation , stil must copy (or move if possible) of elements newly allocated space. still same old vector
regard insertion/removal of elements @ front and/or middle. on other hand, offers better locality of reference, although if blocks used deque
"good large" size, benefit regard caching can argued many apps.
so, question if in light of changes came c++11, deque
should continue remain go / first choice container dynamically sized / random access needs.
josuttis's c++ standard library states: (when use container sec 7.12)
by default, should use vector. has simplest internal data structure , provides random access. thus, data access convenient , flexible, , data processing fast enough.
if insert and/or remove elements @ beginning , end of sequence, should use deque. should use deque if important amount of internal memory used container shrinks when elements removed. also, because vector uses 1 block of memory elements, deque might able contain more elements because uses several blocks.
Comments
Post a Comment