Why no push/pop in front of vector?

后端 未结 4 766
灰色年华
灰色年华 2020-12-21 08:10

In C++, STL, we have template class . We know that it supports O(1) random access, and tail modification. My question is why we don\'

4条回答
  •  甜味超标
    2020-12-21 08:31

    We already have something as you describe in the STL. It is named deque.

    As you wrote there IS actually some overhead. So if you need this functionality and you have no problem with the overhead, use deque. If you do not require it, you do not want the overhead, so it is better to have something that avoids this overhead, named vector.

    And as an addition: vector guarantees that all its elements are stored in contiguous storage locations, so you can apply pointer arithmetic. This is not the case for a circular buffer.

提交回复
热议问题