1

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.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • 4
    Put the string into an input stringstream and read word by word from the stream (like you do now) but push the words into a *stack*. Then in a second loop pop from the stack and print the words. – Some programmer dude Mar 09 '22 at 16:38
  • 1
    "*couldn't find one that takes iterators*" It wouldn't matter if they did; iterators are based on *characters* not words. Reverse iterators would iterate backwards by character. – Nicol Bolas Mar 09 '22 at 16:38
  • @NicolBolas Yes i am aware of that. I was just giving an example to show that at least there should be a constructor that takes iterators. I know that even if we use those iterators we will iterate backwards character by character instead of word by word. Thanks anyways for pointing out the obvious. – Jason Mar 09 '22 at 16:42
  • 4
    It may be obvious to you but Nicol had no way of knowing that and they were just trying to help. If you don't want people to try and help you then I'm not sure why you've asked a question? – Alan Birtles Mar 09 '22 at 16:46
  • @Sean: "*I was just giving an example to show that at least there should be a constructor that takes iterators.*" But that wouldn't help, because there aren't iterators that spit out characters in reverse-word order. I mean, you could in theory write one, but it would probably be more efficient to just create a new string in reverse-word order. – Nicol Bolas Mar 09 '22 at 16:48

2 Answers2

1

You can pass iterators to the istringstream constructor indirectly if you use a temporary string:

#include <sstream>
#include <iostream>

int main()
{
    std::string s{"Hello World\n"};
    std::istringstream ss({s.rbegin(),s.rend()});
    std::string word;
    while(ss >> word)
    {
        std::cout<<word <<" ";
    }
}

Output:

dlroW olleH 

Though thats reversing the string, not words. If you want to print words in reverse order a istringstream alone does not help much. You can use some container to store the extracted words and then print them in reverse:

#include <sstream>
#include <iostream>
#include <vector>

int main()
{
    std::string s{"Hello World\n"};
    std::istringstream ss(s);
    std::string word;
    std::vector<std::string> words;
    while(ss >> word)
    {
        words.push_back(word);
    }
    for (auto it = words.rbegin(); it != words.rend(); ++it) std::cout << *it << " ";
}

Output:

World Hello 
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

Since you added the c++20 tag:

#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include <ranges>


int main()
{
    std::vector<std::string> vec;
    std::string s("some string");
    std::string word;
    std::istringstream ss(s);

    while(ss >> word)
    {
        vec.push_back(word);
    }

    for (auto v : vec | std::views::reverse)
    {
        std::cout << v << ' ';
    }
}
LWimsey
  • 6,189
  • 2
  • 25
  • 53