4

Ok the title is not the best but here is what im looking for.

int arr[] = {3, 4, 5, 6, 7};
int index = 2;
someFunctionICantRemember(int arr, int index);

// result {5, 6, 7, 3, 4}

I saw this function on www.cplusplus.com but i cant find it anymore. It was "built in" as far i can remember.

3
  • 1
    I would call that a cyclic permutation Commented Jun 8, 2012 at 19:22
  • 1
    @mathematician1975 or a circular shifting. you can maybe do a memove but I don't think there's a built in function for this. Commented Jun 8, 2012 at 19:24
  • Frequently when you do this you can use an integer to hold the "start index" of the array and with some other logic not have to swap values. Depending on the situation the code can run a lot faster this way. Commented Jun 8, 2012 at 19:27

1 Answer 1

7

std::rotate (#include <algorithm>).

#include <algorithm>
#include <iterator>
std::rotate(std::begin(arr), std::begin(arr) + index, std::end(arr));

Note that this will rotate the array in place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.