i'm newbie in writing code and i need a little help.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 added to beginning of a vector and values moved from position 1 to 2, from 2 to 3 etc. how do i do that?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
You should consider using
std::deque
. It works alot like astd::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 thestd::deque
s performance for inserting/deleting at the front will be far superior.Use std::vector::insert function accepting iterator to the first element as a target position (iterator before which to insert the element):
Edit: As pointed out in the comments, you could also append the element and perform the rotation to the right: