【问题标题】:std::stringstream Not Converting String To Int Correctlystd::stringstream 未正确将字符串转换为 Int
【发布时间】:2014-03-23 16:54:38
【问题描述】:

我正在尝试将 CharArrayPtr 转换为 int 格式,我已经成功地做到了;

    std::string str;

    for(int i = 0; i < numberofvalues; i++)
    {   



        str = cmemblock[i];
        std::stringstream stream;
        stream <<str;
        int n;
        if (!(stream >>n)){

        }

        cout << n<<endl;
        }

但是,我遇到的问题是,如果我要使用cout &lt;&lt; str;,它将完全按照 .txt 文件中的显示方式显示字符。不幸的是,转换为int格式后输出不如预期,我将在下面演示;

cout << str;

显示这个;

11
22
33
44
55
66
77
88
99

cout << n;

显示这个;

1
1
1
2
2
2
3
3
3
4
4
4
5
5
5
6
6
6
7
7
7
8
8
8
9
9
9
9
9
9
9
9
9
9

我不完全确定为什么会发生这种情况,但我认为我输入的代码可能没有正确编码以解释新行?

【问题讨论】:

  • 我试试看谢谢
  • 我无法重现该问题。有关使用您的代码的示例,请参见此处:ideone.com/Xestb9
  • 我没有使用 C++11,不确定这是否有区别? Visual Studio 2010 恐怕

标签: c++ arrays char int stringstream


【解决方案1】:
for(int i = 0; i < numberofvalues; i++)

问题出在这里,已修改为正确的值,现已解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多