I want to “wrap” around a list/vector in C++ like in Python. Basically I want to shift elements from the end of the list to beginning of the list. I don’t want to have to explicitly make a new list.
In Python I can write something like:
my_list = [1, 2, 3, 4, 5]
#[1, 2, 3, 4, 5]
q = collections.deque(my_list)
q.rotate(3)
#deque([3, 4, 5, 1, 2])
I looked at deque in the STL but I don’t see anything similar to rotate. Seems like there should be an easy way to do this with iterators, or something similar.
You are looking for
std::rotatefrom the standard library, which offers an easy way to do this with iterators.Any forward iterator can be used, so this works on most (sequence) containers.