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

时间:2013-11-12 15:39:50

标签: c++ string reference

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

#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;
        }

    }

}

4 个答案:

答案 0 :(得分:3)

您可以使用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++)

What is a reference variable in 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能够获得他需要的信息和技能。

答案 1 :(得分:1)

#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;
}

答案 2 :(得分:0)

你有很多问题。

    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

答案 3 :(得分:0)

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

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

ReverseString(string info)会将一个空字符串传递给您的ReverseString函数(如果它甚至可以编译 - 看起来不应该这样,因为你在同一范围内有2 info个) 。你想要的是:

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

在您的反向功能中,您只需转到length() / 2

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

最后,cout << ReverseString(info)无用(如果它甚至可以编译),因为ReverseString会返回void。你应该让它返回string(反向字符串)。