I'm trying to reverse a string |
No, you are not. A string is a list of characters. You do successfully reverse the list, but that is not what you do want.
You want to take a list of
words and reverse that list. The words are initially embedded in one string.
The std::cin is an input stream object. You can read from it. The data comes from "outside".
The ss is an input stream object. You can read from it. The data comes from string 'name'.
The istringstream makes it possible to use formatted input stream operations for reading one word at a time.
You could read word at a time from std::cin too, but then the "end of input" becomes different.
@mpark4656: Do consider this variant:
1 2 3 4 5 6 7 8
|
std::string eachWord;
while( ss >> eachWord ) {
if ( reverse.empty() ) {
reverse = eachWord;
} else {
reverse = ...
}
}
|