测试代码
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using std::cin;
using std::cout;
using std::string;
using std::endl;
void ReverseString(string &str,int from,int to) {
while (from < to) {
char c = str.at(from);
str.at(from) = str.at(to);
str.at(to) = c;
from++;
to--;
}
}
void LeftRotationStr(string &str, int m) {
int n = str.length();
m %= n;
ReverseString(str,0, m - 1);
ReverseString(str, m, n - 1);
ReverseString(str, 0, n - 1);
}
int main()
{
string str = "hijklmn";
for (int i = 1; i <= str.length(); i++) {
cout << "移动: " << i << endl;
cout << "before: "<< str << endl;
LeftRotationStr(str, i);
cout << "after: " << str << endl << endl;
}
system("pause");
return 0;
}
测试结果