|
Boost : |
From: Ross Smith (ross.s_at_[hidden])
Date: 2001-08-16 19:46:59
Darin Adler wrote:
>
> While I'm not happy with a split algorithm that only works for a single
> container type, a split that returns a object that stands alone is quite
> different from one that returns two output iterators. I'm not sure the
> output iterator flavor alone would be sufficient to make use of the
> algorithm convenient. To make a possibly-weak analogy, I wouldn't
> typically want a function that generates a string to return two output
> iterators to supply the characters in the string, even though that
> provides additional flexibility and allows me to put the characters into a
> list or write them to a file without constructing the string.
>
> I'd like to see a specific example of what the signature of the output
> iterator flavor would be, and how to use it to construct a vector<string>
> to see if the syntax can be made palatable enough so that it's a pleasure
> to use.
I think you've misunderstood the way the output iterator version was
intended to work. I was the one who originally posted the idea, and my
suggested signature and usage look like this:
template <typename OutputIterator>
OutputIterator split(const std::string& source,
OutputIterator target,
const std::string& delimiters = " \f\n\r\t");
std::string sentence("foo bar zap");
std::vector<std::string> words;
split(sentence, std::back_inserter(words));
It works like the STL algorithms that write into an output sequence,
such as copy() and transform(): it accepts an iterator marking the
beginning of the output sequence, and returns one marking its end. In
this case (unlike copy() or transform()) we don't usually know the
number of elements in advance, so the target argument will normally be
an inserter.
I've been using this function in my library for years, and found it
extremely useful and convenient. I habitually use a similar signature
for any function that can produce a variable-length output sequence.
-- Ross Smith <ross.s_at_[hidden]> The Internet Group, Auckland, New Zealand ======================================================================== "Unix has always lurked provocatively in the background of the operating system wars, like the Russian Army." -- Neal Stephenson
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk