sumit kang sumit kang - 9 months ago 81
C++ Question

Use of >> operator

Can anyone Please explain me the meaning of the following line in the code

while (ss >> temp)

std::string str = "123:234:56:91";

for (int i=0; i<str.length(); i++)
if (str[i] == ':')
str[i] = ' ';

vector<int> array;
stringstream ss(str);
int temp;
while (ss >> temp)

Answer Source

Because ss is a stream, the >> is overloaded to do formatted reading from the stream, depending on the type of the right-hand operand.

So, while(ss >> temp) will read white-space separated integers from the stringstream. This is why you replace the ':' with '' above. When evaluated as a boolean, it will be true if an integer was read and false at the end of the stream.

For more details, see for example here