use*_*546 0 c++ string reference
我正在尝试创建一个函数,该函数将字符串的常量引用作为输入,并在字符串的每个字符向右旋转1个位置后返回字符串.使用引用和指针仍然让我困惑,我不知道如何从常量引用中获取字符串.
string rotate(const string &str){
string *uno = &str;
string dos = rotate(uno.rbegin(), uno.rbegin() + 1, uno.rend());
return dos;}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所得到的,但它没有编译.任何有关如何从常量引用中正确获取字符串的提示将不胜感激.
您不能在不违反const参数合同的情况下就地执行旋转,因此您应该复制输入并返回一个新字符串:
string rotate(const string &str){
string uno = str;
rotate(uno.rbegin(), uno.rbegin() + 1, uno.rend());
return uno;
}
Run Code Online (Sandbox Code Playgroud)
另一个合理的选择是使用 std::rotate_copy
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |