Skip to content

Instantly share code, notes, and snippets.

@airekans
Created May 15, 2015 02:56
Show Gist options
  • Save airekans/624e6fc8a9cdce768b8d to your computer and use it in GitHub Desktop.
Save airekans/624e6fc8a9cdce768b8d to your computer and use it in GitHub Desktop.
Code snippet that help me understand std::rotate
// A reference to the idea is here: http://en.cppreference.com/w/cpp/algorithm/rotate
// Actually its idea is recursion, as shown as follow:
void str_rotate(char* first, char* middle, char* last)
{
char* next = middle;
while (first != middle && next != last)
{
std::iter_swap(first++, next++);
}
if (first == middle && next == last)
{
return;
}
else if (first == middle)
{
str_rotate(first, next, last);
}
else // next == last
{
str_rotate(first, middle, last);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment