C++ 如何重用字符串流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12112259/
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
how to reuse stringstream
提问by Icebone1000
These threads do NOT answer me:
这些线程不回答我:
How do you clear a stringstream variable?
std::ifstream file( szFIleName_p );
if( !file ) return false;
// create a string stream for parsing
std::stringstream szBuffer;
std::string szLine; // current line
std::string szKeyWord; // first word on the line identifying what data it contains
while( !file.eof()){
// read line by line
std::getline(file, szLine);
// ignore empty lines
if(szLine == "") continue;
szBuffer.str("");
szBuffer.str(szLine);
szBuffer>>szKeyWord;
szKeyword
will always contain the first word, szBuffer
is not being reset, can't find a clear example anywhere on how to use stringstream.
szKeyword
将始终包含第一个单词,szBuffer
不会被重置,在任何地方都找不到关于如何使用 stringstream 的清晰示例。
New code after answer:
回答后的新代码:
...
szBuffer.str(szLine);
szBuffer.clear();
szBuffer>>szKeyWord;
...
Ok, thats my final version:
好的,这就是我的最终版本:
std::string szLine; // current line
std::string szKeyWord; // first word on the line identifying what data it contains
// read line by line
while( std::getline(file, szLine) ){
// ignore empty lines
if(szLine == "") continue;
// create a string stream for parsing
std::istringstream szBuffer(szLine);
szBuffer>>szKeyWord;
回答by Marcus Riemer
You didn't clear()
the stream after calling str("")
. Take another look at this answer, it also explains why you should reset using str(std::string())
. And in your case, you could also reset the contents using only str(szLine)
.
clear()
调用后你没有流str("")
。再看看这个答案,它也解释了为什么你应该使用str(std::string())
. 在您的情况下,您也可以仅使用str(szLine)
.
If you don't call clear()
, the flags of the stream (like eof
) wont be reset, resulting in surprising behaviour ;)
如果您不调用clear()
,则eof
不会重置流的标志(如),从而导致令人惊讶的行为;)
回答by James Kanze
It depends what you're doing with it. It's generally easier to just
create a new istringstream
or ostringstream
. To "reset" a stream,
you have to clear its buffer, clear any error flags, reset any
formatting flags, plus the precision and fill, reimbue
it with the
original locale, and not forget any extended formatting information
generated with a value returned from xalloc
. In sum, impossible to
get correct.
这取决于你用它做什么。通常创建一个新的istringstream
或ostringstream
. 要“重置”流,您必须清除其缓冲区,清除任何错误标志,重置任何格式标志,加上精度和填充,imbue
使用原始语言环境重新设置,并且不要忘记使用从返回的值生成的任何扩展格式信息xalloc
. 总之,不可能得到正确的。
And while I'm at it, your loop is wrong, and will probably result in the
last line being processed twice. file.eof()
only has a usable meaning
after the input has failed (and even then, it's not 100% reliable).
What you want is:
当我在做的时候,你的循环是错误的,可能会导致最后一行被处理两次。 file.eof()
只有在输入失败后才有可用的意义(即便如此,它也不是 100% 可靠的)。你想要的是:
std::string line;
while ( std::getline( file, line ) ) {
if ( !line.empty() ) {
std::istringstream buffer( line );
// ...
}
}
(Actually, you probably want to trim trailing white space from the line before the test for empty.)
(实际上,您可能希望在测试为空之前从行中修剪尾随空白。)
回答by James Kanze
ss is stringstream. Use
ss 是字符串流。用
- first step: ss.clear();
- second step: ss.str("");
- 第一步:ss.clear();
- 第二步:ss.str("");
to reuse the stringstream -- completely clear the string stream.
重用字符串流——完全清除字符串流。
回答by Madhav Datt
In most cases, it is easier to create a new istringstream
or ostringstream
instead of resetting the same ones.
在大多数情况下,创建新的istringstream
或ostringstream
重置相同的更容易。
However, if you do want to resent them:
但是,如果您确实想反感他们:
Resent flags of the stream (to avoid unexpected behavior) using
clear ()
.While you can correct the contents of your
stringstream
usingstr ("")
, for efficiency purposes we might preferstr(std::string())
.
使用 重新发送流的标志(以避免意外行为)
clear ()
。虽然您可以更正您
stringstream
使用的内容str ("")
,但为了提高效率,我们可能更喜欢str(std::string())
。
回答by Петър Петров
If you have the stream in a class member,
use unique_ptr<stringstream>
,
then just reset(new stringstream(...))
to reuse it.
如果您在类成员中有流,请使用unique_ptr<stringstream>
, 然后只是reset(new stringstream(...))
重用它。
回答by Nick Dong
Imagine a config file.
想象一个配置文件。
par1=11
par2=22
codes:
代码:
std::string line, strpar1, strpar2;
int par1, par2;
std::ifstream configfile("config.cfg");
std::getline(configfile, line); // first line to variable "line"
std::istringstream sline(line);
while (std::getline(sline, strpar1, '='));
par1 = std::stoi(strpar1); // par1 get 11
bool b = sline.eof(); // true
std::getline(configfile, line); // second line to variable "line"
sline.clear(); //
sline.str(line); // reuse "sline"
b = sline.good(); // true // goodbit is zero, indicating that none of the other bits is set.
b = sine.fail(); // false
b = sline.bad(); // false
b = sline.eof(); // false
while (std::getline(sline, strpar2, '='));
par2 = std::stoi(strpar2); // par2 get 22
goodbit is zero, indicating that none of the other bits is set