Skip to content

Instantly share code, notes, and snippets.

@mochow13
Created September 15, 2018 05:17
Show Gist options
  • Save mochow13/34506bbd05ce16339f2a4508a4bcb0c2 to your computer and use it in GitHub Desktop.
Save mochow13/34506bbd05ce16339f2a4508a4bcb0c2 to your computer and use it in GitHub Desktop.
std::vector<int> collection={1,2,13,5,12,3,4};
// rotating to the left
// element at position 3 of the original collection is now the
// first element of the rotated collection
std::rotate(begin(collection), begin(collection)+3, end(collection));
// rotating to the right
// element at position 2 from the end (or at position 4) is now the
// last element of the rotated collection
std::rotate(rbegin(collection), rbegin(collection)+2, rend(collection));
// Note: in the above code, collection will be changed by both rotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment