Monday, December 11, 2017

C++11: std::rotate()

C++11 added the std::rotate function. Here is an example:

#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
  //                                0  1  2  3  4
  //                                -  -  -  -  -
  int   myArray[]                = {1, 2, 3, 4, 5};
  int * myArrayBeginIterator     = &myArray[0];
  int * myArrayMakeFirstIterator = &myArray[3];
  int * myArrayEndIterator       = &myArray[5];

  rotate(myArrayBeginIterator    ,
         myArrayMakeFirstIterator,
         myArrayEndIterator      );

  for (int * iter  = myArrayBeginIterator;
             iter != myArrayEndIterator  ;
           ++iter                         )
  {
    cout << *iter << " ";
  }

  cout << endl;
  return 0;
}
// Output: 4 5 1 2 3
Reference: http://en.cppreference.com/w/cpp/algorithm/rotate

No comments:

Post a Comment