简体   繁体   English

C ++将字符串转换为int

[英]c++ converting string to 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;
}

I have this for-loop which checks each character of the string and converts every number in this string to be a int. 我有这个for循环,它检查字符串的每个字符并将该字符串中的每个数字转换为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? 但是由于某种原因,atoi函数执行了两次,所以当我退出它时,出于某种原因它会显示两次...为什么呢?

example: INPUT 3 3 -3 9 5 例如:INPUT 3 3 -3 9 5
-8 -2 9 7 1 -8 -2 9 7 1
-7 8 4 4 -8 -7 8 4 4 -8
-9 -9 -1 -4 -8 -9 -9 -1 -4 -8

OUTPUT 3030-309050 -80-20907010 输出3030-309050 -80-20907010
-70804040-80 -70804040-80
-90-90-10-40-80 -90-90-10-40-80

It's displaying a zero for all nonrecognized characters, because atoi returns 0 when given a non-numeric string (like a space!) 它对所有无法识别的字符都显示为零,因为当给定非数字字符串(例如空格!)时, atoi返回0

However, what you want to do, is shockingly simple: 但是,您要执行的操作非常简单:

std::stringstream ss(sLine);
int num;
while(ss >> num) {
    cout << num;
}

Move this: 移动这个:

const char* testing = sNumber.c_str();
int num = atoi(testing);
cout << num;

Below the last } in the code you pasted, ie out of the for-loop. 在您粘贴的代码的最后一个} ,即,位于for循环之外。 Currently you get a separate printout for every character in sLine because it's executed on every iteration of the loop. 当前,由于sLine每个字符都在循环的每次迭代中执行,因此您将获得单独的打印输出。 (The last character in sLine may be a linefeed so this can occur even if you think you wrote only one digit.) sLine的最后一个字符可能是换行符,因此即使您认为只写了一位数字,也会发生这种情况。)

Edit: Also move the declaration of sNumber above the for-loop. 编辑: sNumber的声明sNumber for循环上方

You may also want to change if (sLine[l] == '-') to if (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. 您可能还想将if (sLine[l] == '-')更改为if (sLine[l] == '-' && (l + 1) < sLine.length())以便您无法访问如果破折号是该行的最后一个字符,则字符串的末尾。

You may also want to rename the variable l to something that looks less like a 1 . 您可能还想将变量l重命名为看起来不太像1东西。 =) =)

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). 您可能还想重新考虑一下这是否是正确的方法(通常,如果简单的事情变得如此复杂,很可能您做错了)。

You output extra 0 for the characters which are not digits. 您为非数字字符输出额外的0 The problem is that atoi returns 0 when it cannot convert the input, so your whitespaces are printed as zeroes. 问题是atoi不能转换输入时会返回0,因此您的空格打印为零。

That seems like a painful way to recreate the wheel. 这似乎是一种痛苦的方式来重新创建轮子。 You'd be better off using a stringstream to parse this. 您最好使用stringstream对此进行解析。

std::stringstream strm(sLine);
int num;
while(strm >> num)
{
    std::cout << num << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM