Q:

c++ string manipulation reversal

I am currently doing c++ and am going through how to take in an sentence through a string and reverse the words (This is a word......word a is This etc)
I have looked at this method:
static string reverseWords(string const& instr)
{
    istringstream iss(instr);
    string outstr;
    string word;
    iss >> outstr;
    while (iss >> word)
    {
        outstr = word + ' ' + outstr;
    }
    return outstr;
}

int main()
{
    string s;
    cout << "Enter sentence: ";
    getline(cin, s);

    string sret = reverseWords(s);
    cout << reverseWords(s) << endl;

    return 0;
}

I have gone through the function and kind of understand but I am a bit confused as to EXACTLY what is going on at 
iss >> outstr;

while (iss >> word)
    {
        outstr = word + ' ' + outstr;
    }
    return outstr;

Can anybody explain to me the exact process that is happening that enables the words to get reversed?
Thank you very much

A:

iss is an istringstream, and istringstreams are istreams.
As an istream, iss has the operator>>, which reads into strings from its string buffer in a whitespace delimeted manner.  That is to say, it reads one whitespace separated token at a time.
So, given the string "This is a word", the first thing it would read is "This".  The next thing it would read would be "is", then "a", then "word".  Then it would fail.  If it fails, that puts iss into a state such that, if you test it as a bool, it evaluates as false.
So the while loop will read one word at a time.  If the read succeeds, then the body of the loop appends the word to the beginning of outstr.  If it fails, the loop ends.

