我有一个正在读取的 stringstream
实例。在从流中获取数据的某个时刻,我需要读取一个可能存在也可能不存在的标识符。逻辑是这样的:
std::string identifier;
sstr >> identifier;
if( identifier == "SomeKeyword" )
//process the the rest of the string stream using method 1
else
// back up to before we tried to read "identifier" and process the stream using method 2
如何实现上述逻辑?
最佳答案
std::string identifier;
std::stringstream::pos_type pos = sstr.tellg();
sstr >> identifier;
if (identifier == "SomeKeyword")
{
//process the rest of the string stream using method 1
}
else
{
// back up to before we tried to read "identifier
sstr.seekg(pos);
// process the stream using method 2
}
关于c++ - 如何从字符串流中的同一位置读取两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34050729/