使用引用传递方法的反向字符串代码不起作用
Reverse string code using passing by reference method not working
请告诉我为什么反转输入字符串的代码会给我带来各种错误。
#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
- 编译包含字符串的代码时遇到问题
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 我无法在 qt 的C++代码中定义字符串
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 有人可以解释一下这段代码如何能够反转字符串
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 如何用符合C++核心准则的代码替换C风格的字符串解析
- C++/CLI访问字符串::来自非CLI代码的格式
- MSVC UTF8字符串编码使用了不正确的代码点
- C++指向对象的指针堆数组中,如何将字符串传递给特定对象?有一些代码请看一下:
- 将浮点数转换为字符串时如何加快此代码的速度?
- C++代码,用于在用户插入的字符串的每个元音之后插入一对字符
- 如何获取包含 NULL 字符的外壳代码字符串的长度
- 字符串代码不起作用
- C++字符串代码点和代码单元的任何良好解决方案
- 输入验证,需要数字,而不是字符串.代码导致无限循环
- 使用引用传递方法的反向字符串代码不起作用
- 这个反向字符串代码正确吗