stringstream string to int

后端 未结 4 821
独厮守ぢ
独厮守ぢ 2021-02-05 12:17

The C++ code below does int to string and a string to int conversions. Then, it repeats these steps again. The stringst

4条回答
  •  执笔经年
    2021-02-05 12:55

    After reading i3 from the stream, the eof bit of the stream gets set. This is because reading an integer from the stream makes the stream read until either it reads a nondigit character or it runs out of characters to read. When the latter that happens, the eof bit is set.

    For example, I can change this line,

    stream1.str(s2);
    

    to this,

    stream1.str(s2 + " ");
    

    so when stream1 >> i3; is executed, it will encounter a nondigit character (' '), stop reading and not set the eof bit.

    You have to unset the eof bit by .clear or some other method before attempting to read from it.

提交回复
热议问题