简体   繁体   English

stringstream string to int

[英]stringstream string to int

The C++ code below does int to string and a string to int conversions. 下面的C ++代码做intstring和一个stringint转换。 Then, it repeats these steps again. 然后,它再次重复这些步骤。 The stringstream to int line stream1 >> i3; stringstreamint线stream1 >> i3; is breaking 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("") 我解决了在.str(“”)之前插入.clear()的问题

Have a look here: How to reuse an ostringstream? 看看这里: 如何重用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 (ie stream1.eof() returns true ), but you never clear it. 问题是在提取整数时设置了流的EOF标志(即stream1.eof()返回true ),但是你永远不会清除它。 Inserting a call to stream1.clear() after extraction fixes your issue. 提取后插入对stream1.clear()的调用可以解决您的问题。

After reading i3 from the stream, the eof bit of the stream gets set. 从流中读取i3后,流的eof位被设置。 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. 当后者发生时,eof位置位。

For example, I can change this line, 例如,我可以更改此行,

stream1.str(s2);

to this, 对此,

stream1.str(s2 + " ");

so when stream1 >> i3; 所以当stream1 >> i3; is executed, it will encounter a nondigit character (' '), stop reading and not set the eof bit. 执行时,它会遇到一个非数字字符(''),停止读取而不设置eof位。

You have to unset the eof bit by .clear or some other method before attempting to read from it. 在尝试从中读取之前,您必须通过.clear或其他方法取消eof位。

I'm a C guy most of the day, so I would simply use atoi() do to the conversion: 我大部分时间都是C家伙,所以我只想使用atoi()做转换:

#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. 我正在做的是从stringstream检索一个std::string ,并从中获取一个const char*作为atoi()参数,它将C字符串转换为整数。

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

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