开发者

stringstream string to int

The C++ code below does int to string and a string to int conversions. Then, it repeats these steps again. The stringstream to int line stream1 >> i3; is br开发者_如何转开发eaking the code. What am I missing here?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
 int i1 = 101;
 int i2 = 202;
 int i3 = 0;
 string s1 = "";
 string s2 = "";
 stringstream stream1;

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

   //int to string
   stream1.str("");
   cout << "i1 " << i1 << endl;
   stream1 << i1;
   s1 = stream1.str();
   cout << "s1 " << s1 << endl;

   //int to string
   cout << "i2 " << i2 << endl;
   stream1.str("");
   stream1 << i2;
   s2 = stream1.str();
   cout << "s2 " << s2 << endl;

   //string to int
   stream1.str("");
   stream1.str(s2);
   stream1 >> i3;
   //line above causes s1 and s2 to get messed up during 2nd time in for loop
   cout << "i3-- " << i3 << endl;

  }
  return 0;
 }


I tested your code and could reproduce the problem. I solved it inserting a .clear() before .str("")

Have a look here: How to reuse an ostringstream?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
 int i1 = 101;
 int i2 = 202;
 int i3 = 0;
 string s1 = "";
 string s2 = "";
 stringstream stream1;

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

   //int to string
   stream1.clear();
   stream1.str("");
   cout << "i1 " << i1 << endl;
   stream1 << i1;
   s1 = stream1.str();
   cout << "s1 " << s1 << endl;

   //int to string
   cout << "i2 " << i2 << endl;
   stream1.clear();
   stream1.str("");
   stream1 << i2;
   s2 = stream1.str();
   cout << "s2 " << s2 << endl;

   //string to int
   stream1.clear();
   stream1.str("");
   stream1.str(s2);
   stream1 >> i3;
   //line above causes s1 and s2 to get messed up during 2nd time in for loop
   cout << "i3-- " << i3 << endl;

  }
  return 0;
 }


The problem is that the stream's EOF flag is being set while extracting the integer (i.e. stream1.eof() returns true), but you never clear it. Inserting a call to stream1.clear() after extraction fixes your issue.


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.


I'm a C guy most of the day, so I would simply use atoi() do to the conversion:

#include <stdlib.h> // header for atoi()

//stream1 >> i3;  // replace this for the line below
i3 = atoi(stream1.str().c_str());

cout << "i3-- " << i3 << endl;

What I'm doing is retrieving a std::string from the stringstream, and from that, getting a const char* to be used as argument for atoi(), which converts a C string to an integer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜