C++---字符串循環右移

#include
#include
#include
using namespace std;

string right_r_func(const string& s,unsigned int n)
{
string ret = “”;
unsigned int pos = 0;
//找位置
//切
//
n = n%s.length();
pos = s.length() - n;
ret = s.substr(pos);
ret += s.substr(0,pos);

return ret;

}

int main()
{

string ss = right_r_func("abcdefg",8);
cout<< ss <<endl;
return 0;

}

//利用操作符重載方式進行實現
#include
#include
#include
using namespace std;

string operator >>(const string& s,unsigned int n)
{
string ret = “”;
unsigned int pos = 0;
//找位置
//切
//
n = n%s.length();
pos = s.length() - n;
ret = s.substr(pos);
ret += s.substr(0,pos);
return ret;
}
int main()
{

string ss = "abcdefg";
ss = ss >> 8;
cout<< ss <<endl;
return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章