bitrex
2018-10-18 23:23:47 UTC
I need an implementation of rotate for a microprocessor platform where I
have C++11 compiler but really nothing else available to work with. I'd
like to be able to rotate the elements of a short array of characters of
arbitrary length e.g. ABCD -> BCDA -> CDAB etc.
Something like the following should be good enough, but what's good to
use for "swap"? "algorithm" is a non-starter here, too.
#include <algorithm>
string rotate(string s) {
for (int i = 1; i < s.size(); i++)
swap(s[i-1], s[i]);
return s;
}
have C++11 compiler but really nothing else available to work with. I'd
like to be able to rotate the elements of a short array of characters of
arbitrary length e.g. ABCD -> BCDA -> CDAB etc.
Something like the following should be good enough, but what's good to
use for "swap"? "algorithm" is a non-starter here, too.
#include <algorithm>
string rotate(string s) {
for (int i = 1; i < s.size(); i++)
swap(s[i-1], s[i]);
return s;
}