我有一个名为“is”的 istringstream,其中包含字符串“101a0101”。我的代码在这里:
cout << is.str() << endl;
char bit;
while (is >> bit) {
if (bit == '0') /* do stuff */;
else if (bit == '1') /* do stuff */;
else break;
}
cout << is.str() << endl;
这是我的输出
output:
101a0101
101a0101
为什么我的 istringstream 不消耗字符?
最佳答案
使用str()
访问流的内容将返回缓冲区中的整个字符序列。如果需要访问流中未读的子序列,可以将 substr()
与 tellg()
结合使用:
std::string unread = is.str().substr(is.tellg());
std::cout << unread; // "a0101"
关于c++ - istream 不消耗字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923533/