I’d like to know if there is a shorter / simpler way to:
- Split the incoming string by words
- Write the tokens in reverse order to stdout
There are two restrictions: no libraries and no loops
#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <deque>
void list_string_elements(std::string s) {
using namespace std;
istringstream iss (s);
deque<string> v;
copy(istream_iterator<string>(iss), istream_iterator<string>(), front_inserter(v));
copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
}
Slight abbreviation, we can get rid of a copy thanks to the iterator constructor of
deque:Note the extra parens around
istream_iterator<string>(), to avoid a most vexing parse.