Friday, 15 August 2014

c++ - Why is std::deque not a vector with memory reserved before index 0? -



c++ - Why is std::deque not a vector with memory reserved before index 0? -

as far understand, motivation behind deque provide random-access container efficient push_front.

commonly cited advantages of vector compared deque include faster traversal , at(), c compatibility, guarantees contiguous memory. deque not, since collection of chunks of memory, each holding number of values.

i'm confused. why not deque built vector memory reserved before index 0 in add-on memory reserved after index size-1 ? guarantee contiguous memory, enable efficient push_front , avoid additional indirection when dereferencing iterators.

to minimize shifting during insertion, contained values shifted depend on insertion point. if inserting @ index n beingness before size()/2, shift values n left. otherwise shift right values after n.

what did miss of import deque collection of arrays of values , not 1 big array ?

according wikipedia, you're describing indeed 1 possible implementation, @ to the lowest degree in general.

however, c++ standard imposes requirements prohibit implementation std::deque; [deque.modifiers] states:

an insertion @ either end of deque ... has no effect on validity of references elements of deque.

(thanks @benjaminlindley!)

c++ vector stl deque

No comments:

Post a Comment