c++将字符串转换为 int
//sLine is the string
for(int l = 0; l < sLine.length(); l++)
{
string sNumber;
if(sLine[l] == '-')
{
sNumber.push_back(sLine[l]);
sNumber.push_back(sLine[l + 1]);
l++;
}
else if(sLine[l] != '\t')
{
sNumber.push_back(sLine[l]);
}
const char* testing = sNumber.c_str();
int num = atoi(testing);
cout << num;
}
我有这个 for 循环,它检查字符串的每个字符并将该字符串中的每个数字转换为 int。但由于某种原因,atoi 函数执行了两次,所以当我计算它时,它会出于某种原因显示两次......这是为什么?
例子: 输入 3 3 -3 9 5
-8 -2 9 7 1
-7 8 4 4 -8
-9 -9 -1 -4 -8
输出 3030-309050 -80-20907010
-70804040-80
-90-90-10-40-80
//sLine is the string
for(int l = 0; l < sLine.length(); l++)
{
string sNumber;
if(sLine[l] == '-')
{
sNumber.push_back(sLine[l]);
sNumber.push_back(sLine[l + 1]);
l++;
}
else if(sLine[l] != '\t')
{
sNumber.push_back(sLine[l]);
}
const char* testing = sNumber.c_str();
int num = atoi(testing);
cout << num;
}
I have this for-loop which checks each character of the string and converts every number in this string to be a int. But for some reason, the atoi function is doing it twice so when I cout it, it displays it twice for some reason... Why is that?
example:
INPUT
3 3 -3 9 5
-8 -2 9 7 1
-7 8 4 4 -8
-9 -9 -1 -4 -8
OUTPUT
3030-309050
-80-20907010
-70804040-80
-90-90-10-40-80
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它为所有无法识别的字符显示零,因为当给定非数字字符串(如空格!)时,atoi 返回 0
但是,您想要做的是令人震惊的简单的:
It's displaying a zero for all nonrecognized characters, because
atoi
returns0
when given a non-numeric string (like a space!)However, what you want to do, is shockingly simple:
移动它:
在您粘贴的代码中最后一个
}
下面,即移出 for 循环。目前,您会为sLine
中的每个字符获得单独的打印输出,因为它在循环的每次迭代中执行。 (sLine
中的最后一个字符可能是换行符,因此即使您认为只写了一位数字,也可能会发生这种情况。)编辑:同时移动
sNumber
位于 for 循环上方。您可能还想将
if (sLine[l] == '-')
更改为if (sLine[l] == '-' && (l + 1) < ; sLine.length())
因此,如果破折号是该行的最后一个字符,则不会访问超出字符串末尾的内容。您可能还想将变量
l
重命名为看起来不太像1
的名称。 =)您可能还想重新考虑这是否是正确的方法(通常,如果一个简单的事情变得如此复杂,很可能您做错了)。
Move this:
Below the last
}
in the code you pasted, i.e. out of the for-loop. Currently you get a separate printout for every character insLine
because it's executed on every iteration of the loop. (The last character insLine
may be a linefeed so this can occur even if you think you wrote only one digit.)Edit: Also move the declaration of
sNumber
above the for-loop.You may also want to change
if (sLine[l] == '-')
toif (sLine[l] == '-' && (l + 1) < sLine.length())
so you don't access beyond the end of the string if the dash is the final character on the line.You may also want to rename the variable
l
to something that looks less like a1
. =)You may also want to rethink if this is the right way to do this at all (usually if a simple thing gets this complicated, chances are you're doing it wrong).
对于非数字字符,您输出额外的
0
。问题是atoi
返回 0无法转换输入,因此您的空格将被打印为零。You output extra
0
for the characters which are not digits. The problem is thatatoi
returns 0 when it cannot convert the input, so your whitespaces are printed as zeroes.这似乎是一种重新造轮子的痛苦方法。您最好使用字符串流来解析它。
That seems like a painful way to recreate the wheel. You'd be better off using a stringstream to parse this.