使用引用传递方法的反向字符串代码不起作用

Reverse string code using passing by reference method not working

本文关键字:字符串 代码 不起作用 方法 引用      更新时间:2023-10-16

请告诉我为什么反转输入字符串的代码会给我带来各种错误。

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
void ReverseString(string &aString);
int main(){
    string info;
    cout << "What's your string?" << endl;
    getline(cin, info);
    ReverseString(info);
    cout << ReverseString(string info) << " compare with: " << info << endl;
    system("pause");
    return 0;
}
void ReverseString(string &aString){
for(int i = 0; i < aString.length(); i++)
    {
        string temp = 0; // initialize temporary string
        temp = temp + aString.at(aString.length() - 1 - i); // hold temporary string
        if(i => aString.length()) /*assign temp string to aString when all chars are processed*/
        {
            temp = &aString;
        }
    }
}

嗨,使用STL 可以大大简化代码

例如:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    std::string str = "Hello World";
    cout << str << endl; 
    std::reverse(str.begin() , str.end());
    cout << str << endl;
   return 0;
}

如果这不适合你的需求,请告诉我,因为还有其他一些方法可以做到这一点。

无STL:

我在下面提供了一些对您的代码的更正/更改。然而,您可能需要查看一些关于引用变量的文档,以了解它的工作原理,例如:

http://www.cprogramming.com/tutorial/references.html

http://www.thegeekstuff.com/2013/05/cpp-reference-variable/

http://en.wikipedia.org/wiki/Reference_(C++(

C++中的引用变量是什么?

http://www.tutorialspoint.com/cplusplus/cpp_references.htm

正确使用引用和指针是C++的一个重要组成部分,如果使用得当,它可以实现语言中一些最强大的功能,如果使用不当,则会带来严重的头痛和精神创伤,因此,牢牢掌握它们是值得的,甚至是必不可少的。

即便如此,偶尔也会出现这种奇怪的误用。:(

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
void ReverseString(string &aString);
int main(){
    string info;
    cout << "What's your string?" << endl;
    getline(cin, info);
    cout << info << " compare with: ";
    ReverseString(info);
    cout << info << endl;
    system("pause");
    return 0;
}
void ReverseString(string &aString)
{
    int len = aString.length();
    string temp = aString;// initialize temporary string
    aString ="";
    for(int i = 0; i < len; i++)
    {
        aString += temp[len - (1+ i)]; // assigns the reversed value to the referenced string
    }
}

我刚刚注意到下面来自@zac howland的一句话:没错,我把代码作为一个说明性的部分留下了。如果对此进行了一些阅读,并进行了大量的实验,我希望NewProgrammer将获得前进所需的信息和技能。

#include<iostream>
#include<string>
#include <algorithm>
using namespace std;
string info;
string rvrs(string &str)
{
    std::reverse(str.begin(),str.end());
    return str;
}
int main()
{
    cout<<"What is your string :: ";
    getline(cin,info);
    cout<<rvrs(info);
    cout<<endl; 
return 0;
}

除了逻辑错误之外,还有一些语法错误:

cout << ReverseString(string info) << " compare with: " << info << endl;

ReverseString(string info)将向ReverseString函数传递一个空字符串(如果它甚至进行编译——这看起来不应该,因为在同一范围内有两个info(。你想要的是:

cout << ReverseString(info) << " compare with: " << info << endl;

在反向函数中,您只需要转到length() / 2

由于您是通过引用传递的,因此您在函数中对字符串所做的更改将反映在传递给它的对象中。也就是说,原始的info将被反转。如果您希望它对副本进行操作,则需要通过副本传递,而不是通过引用传递。

最后,由于ReverseString返回void,所以cout << ReverseString(info)没有用处(如果它甚至进行编译的话(。您应该让它返回一个string(反转的字符串(。

您有很多问题。

    string temp = 0; // initialize temporary string

将字符串初始化为0实际上没有意义。只要string temp;就可以了。

    temp = temp + aString.at(aString.length() - 1 - i); // hold temporary string

我不太会这样做,但我想它应该起作用。

    if(i => aString.length()) 

这种情况似乎没有道理。循环被定义为i从0到字符串-1的长度进行迭代,因此它永远不能大于或等于字符串长度。

    /*assign temp string to aString when all chars are processed*/
    {
        temp = &aString;
    }

这里的代码与注释不匹配。注释说您要分配给aString,但代码为temp分配了一些内容。这个评论可能更接近你真正想要的。但是,您仍然需要修复该条件,并且可能希望在循环执行完成后进行修复。所以在伪代码中,你最终会得到这样的东西:

for (all characters in the string)
    add the next character in the string to the end of temp
assign temp back to the original string