I'd like to know if there is a shorter / simpler way to:
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
:
void list_string_elements(std::string s) {
using namespace std;
istringstream iss (s);
deque<string> v(istream_iterator<string>(iss), (istream_iterator<string>()));
copy(v.rbegin(),v.rend(),ostream_iterator<string>(cout,"\n"));
}
Note the extra parens around istream_iterator<string>()
, to avoid a most vexing parse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With