How can I insert element into beginning of vector?

后端 未结 4 1560
天涯浪人
天涯浪人 2021-01-03 18:49

I need to insert values into the beginning of a std::vector and I need other values in this vector to be pushed to further positions for example: something adde

4条回答
  •  感情败类
    2021-01-03 19:19

    You should consider using std::deque. It works alot like a std::vector but you can add and remove items from both the front and the end.

    It does this by dividing the internal storage up into smaller blocks. You still have random-access iterators with good lookup speed.

    If your container is small it should be fine to use the std::vector approach but if you are storing large amounts of data the std::deques performance for inserting/deleting at the front will be far superior.

提交回复
热议问题