sumit kang
sumit kang

Reputation: 476

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)
       array.push_back(temp); 

Upvotes: 1

Views: 80

Answers (1)

user1812457
user1812457

Reputation:

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

Upvotes: 5

Related Questions