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.

user1435915
  • 185
  • 1
  • 1
  • 7
  • 1
    I would call that a cyclic permutation – mathematician1975 Jun 08 '12 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. – Hans Z Jun 08 '12 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. – NominSim Jun 08 '12 at 19:27

1 Answers1

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.

ecatmur
  • 152,476
  • 27
  • 293
  • 366