I have a istringstream called 'is' and contains the string "101a0101". My code here:

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;

Here's my output

output:   
101a0101
101a0101

Why isn't my istringstream consuming characters?

有帮助吗?

解决方案

Accessing the content of the stream using str() will return you then entire character sequence in the buffer. If you need to access the unread subsequence of the stream, you can use substr() with tellg():

std::string unread = is.str().substr(is.tellg());
std::cout << unread; // "a0101"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top