I am learning C++ using recommended C++ books. In particular, i read about std::istringstream and saw the following example:
std::string s("some string");
std::istringstream ss(s);
std::string word;
while(ss >> word)
{
std::cout<<word <<" ";
}
Actual Output
some string
Desired Output
string some
My question is that how can we create the above std::istringstream object ss using the reversed string(that contains the word in the reverse order as shown in the desired output)? I looked at std::istringstream's constructors but couldn't find one that takes iterators so that i can pass str.back() and str.begin() instead of passing a std::string.
You can pass iterators to the
istringstreamconstructor indirectly if you use a temporary string:Output:
Though thats reversing the string, not words. If you want to print words in reverse order a
istringstreamalone does not help much. You can use some container to store the extracted words and then print them in reverse:Output: