Вопрос

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