-
move element in a vector
Hello
I have an easy question.
How can I move element in a vector so that it will be first in the list and other elements will move one place forward?
What is the easiest way? Should I better go for another container for this kind of purpose?
Thanks for help
-
If you're doing this a lot it may be expensive with a vector, but other containers have other disadvantages so it depends on the rest of the circumstances.
As far as how you do it, you would probably have to erase the element from the vector and then insert it at position 0.
-
-
A deque might be more happy about insertions in the beginning.
But it seems that you also want to take the element out of the container first (not necessarily from the end?) which might be something a list is better at.
The choice of container might depend on questions such as do you have a lot of data and are you planning to do this operation often, and what else do are you going to do with it.
If a vector is good, I guess using rotate on the range from begin to the element you want to get to the front sounds OK.