windows 重用 stringstream 对象的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2144144/
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
Problem with re-using a stringstream object
提问by kaykun
I'm trying to use safe practices in handling input with numbers only in C++, so I use a stringstream object as so:
我试图在仅在 C++ 中使用数字处理输入时使用安全做法,因此我使用 stringstream 对象,如下所示:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int first, second;
string input;
stringstream sstream;
cout << "First integer: ";
getline(cin, input);
sstream.str(input);
sstream >> first;
cout << first << endl; //display user input in integers
cout << "Second integer: ";
getline(cin, input);
sstream.str(input);
sstream >> second;
cout << second << endl; //display user input in integers
getline(cin, input); //pause program
return 0;
}
However, the second time around it seems to give the variable 'second' an arbitrary value. This is the output:
但是,第二次它似乎给变量“second”一个任意值。这是输出:
First integer: 1
1
Second integer: 2
2293592
If I declare two stringstream objects and use them respectively for both variables it seems to work fine. Does this mean that I cannot re-use a stringstream object in the way I'm trying to do? In my real program I intend to handle much more than two input values from the user, so I just want to make sure if there's another way instead of making multiple stringstream objects. I doubt it's of great relevance but I'm on Windows XP and I'm using MinGW as my compiler.
如果我声明两个 stringstream 对象并将它们分别用于两个变量,它似乎工作正常。这是否意味着我不能以我尝试的方式重新使用 stringstream 对象?在我的实际程序中,我打算处理来自用户的两个以上的输入值,所以我只想确定是否有另一种方法而不是制作多个 stringstream 对象。我怀疑它是否有很大的相关性,但我使用的是 Windows XP 并且我使用 MinGW 作为我的编译器。
I greatly appreciate any help.
我非常感谢任何帮助。
回答by Max Shawabkeh
Use sstream.clear();
after sstream >> first;
.
sstream.clear();
后使用sstream >> first;
。
回答by James McNellis
You need to reset the state of the stringstream
. Generally, this involves two steps: clearing the buffer:
您需要重置stringstream
. 通常,这包括两个步骤:清除缓冲区:
sstream.str("");
and resetting the error state flags:
并重置错误状态标志:
sstream.clear();
If you don't clear the buffer, if you get an input like "123abc" then "abc" will still be in the stream when you try to read from it the next time.
如果您不清除缓冲区,如果您收到类似“123abc”的输入,那么当您下次尝试从中读取时,“abc”仍将在流中。
You should also make sure to test the fail state of the stream (sstream.fail()
) to ensure that the extraction was successful. If you want to be sure that the user only entered an integer (i.e., you want to prevent the user from inputting, say, "123abc", then you should test to make sure sstream.eof()
is true.
您还应该确保测试流 ( sstream.fail()
)的失败状态以确保提取成功。如果您想确保用户只输入了一个整数(即,您想阻止用户输入,比如“123abc”,那么您应该测试以确保sstream.eof()
为真。
回答by RC.
A better way to do this conversion between datatypes would be to use boost::lexical_cast
.
在数据类型之间进行这种转换的更好方法是使用boost::lexical_cast
.
Info and examples can be found at the Boost Site.
可以在Boost 站点找到信息和示例。
Below is an example of doing an int to string conversion and back (string to int) such as what you're doing in your program.
下面是执行 int 到字符串转换和返回(字符串到 int)的示例,例如您在程序中执行的操作。
#include <string>
#include <boost/lexcal_cast.hpp>
int main(int argc, char *argv[])
{
int i = 42;
std::string s = boost::lexical_cast<std::string>(i);
int j = boost::lexical_cast<int>(s);
return 1;
}
回答by OwnWaterloo
cout << "First integer: ";
getline(cin, input);
sstream.str(input);
sstream >> first; // state of sstream may be eof
cout << "Second integer: ";
getline(cin, input);
sstream.str(input);
sstream.clear(); // clear eof state
sstream >> second; // input from sstream