Created
May 15, 2015 02:56
-
-
Save airekans/624e6fc8a9cdce768b8d to your computer and use it in GitHub Desktop.
Code snippet that help me understand std::rotate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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