Just as it's possible to write something like:
vector<string> vec; // ...
copy(begin(vec), end(vec), ostream_iterator<string>(cout, "\n"));
I propose it be possible to write something like:
vector<string> vec;
istringstream iss; // some input stream with several lines of data
copy(istream_iterator<string>(iss, "\n"), istream_iterator<string>(), back_inserter(vec)); // ala getline, but supplies an arbitrary number of patterns
The new second parameter
(optional) for
the istream_iterator constructor represents a collection of all
delimiter chars. This would default to all whitespace characters.
Pros:- Ability to specify arbitrary tokenization delimiters without the need for extensive ctype specialization or something else nasty
- Reading something like an IP: 127.0.0.1 into its components can now be specified in terms of istream_iterator's delimiter
- Relatively familiar approach ala ostream_iterator and getline
- Backward compatible and non-intrusive
Cons:- Locale problems?
- Only supplied for istream_iterator<string> specialization? (could also work with other types!)
istringstream iss{ "0.1|0.2|0.3" }; // current istream_iterator will choke on this
copy(istream_iterator<double>(iss, "|"), istream_iterator<double>(), ostream_iterator<double>(cout, " ")); // prints "0.1 0.2 0.3"
I know that each of these can be implemented with custom input iterators; that's not the point.