【发布时间】:2022-01-07 16:50:38
【问题描述】:
有没有办法找到istringstream::operator >>提取的token的起始位置?
例如,我当前检查tellg() (run online) 的尝试失败:
string test = " first \" in \\\"quotes \" last";
istringstream strm(test);
while (!strm.eof()) {
string token;
auto startpos = strm.tellg();
strm >> quoted(token);
auto endpos = strm.tellg();
if (endpos == -1) endpos = test.length();
cout << token << ": " << startpos << " " << endpos << endl;
}
所以上面程序的输出是:
first: 0 8
in "quotes : 8 29
last: 29 35
结束位置很好,但开始位置是通向标记的空白的开始。我想要的输出是这样的:
first: 3 8
in "quotes : 13 29
last: 31 35
这是带有位置的测试字符串供参考:
1111111111222222222233333
01234567890123456789012345678901234 the end is -1
first " in \"quotes " last
^--------------------^-----^ the end positions i get and want
^-------^--------------------^------ the start positions i get
^---------^-----------------^---- the start positions i *want*
在使用istringstream时,是否有任何直接的方法可以检索此信息?
【问题讨论】:
-
请澄清...您提供了程序的示例输出,然后显示了您希望看到的输出示例,与第一个示例相同。
-
@paddy 啊,编辑造成的附带损害。感谢您发现这一点。一瞬间....固定。对此感到抱歉。
标签: c++ parsing c++17 istringstream