You can test the return value of >>
.
while (ss >> t) {
lis.push_back(t);
}
It's not specified to read circularly. It's ss << s
appending "1 2 3 4"
to the end of the stream.
Before the 1st loop:
""
After 1st ss << s
:
"1 2 3 4"
After 1st ss >> t
:
" 2 3 4"
After 2nd ss << s
:
" 2 3 41 2 3 4"
Then it's clear why you get 1 2 3 41 2 3 41 2 3 41
if there is no trailing space in s
.