C++ stringstream 字符串转int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5476616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:14:33  来源:igfitidea点击:

stringstream string to int

c++

提问by user584583

The C++ code below does intto stringand a stringto intconversions. Then, it repeats these steps again. The stringstreamto intline stream1 >> i3;is breaking the code. What am I missing here?

下面的 C++ 代码执行inttostring和 a stringtoint转换。然后,它再次重复这些步骤。在stringstreamint线stream1 >> i3;更是打破了代码。我在这里缺少什么?

#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;
 }

回答by Javi R

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;
 }

回答by ildjarn

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.

问题是在提取整数(即stream1.eof()返回true)时设置了流的 EOF 标志,但您永远不会清除它。stream1.clear()在提取后插入呼叫可解决您的问题。

回答by tiftik

After reading i3from 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.

i3从流中读取后,流的 eof 位被设置。这是因为从流中读取整数会使流读取,直到它读取非数字字符或用完要读取的字符为止。当后者发生时,eof 位被设置。

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.

所以当stream1 >> i3;被执行时,它会遇到一个非数字字符(' '),停止读取并且不设置 eof 位。

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

.clear在尝试从中读取之前,您必须通过或其他方法取消设置 eof 位。

回答by karlphillip

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

我大部分时间都是 C 语言的人,所以我会简单地使用atoi()do 来转换:

#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::stringfrom the stringstream, and from that, getting a const char*to be used as argument for atoi(), which converts a C stringto an integer.

我正在做的是std::string从 中检索 a stringstream,然后从中获取 aconst char*作为 for 的参数atoi(),它将C 字符串转换为整数。