我有一个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
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现上述逻辑?
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
}
Run Code Online (Sandbox Code Playgroud)