Boost logo

Boost Users :

Subject: Re: [Boost-users] tokenize string delimiter
From: Matthias Vallentin (vallentin_at_[hidden])
Date: 2010-02-11 11:43:48


On Wed, Feb 10, 2010 at 04:57:41PM -0800, Marshall Clow wrote:
> Can anyone tell me why iter_split (and split, etc) requires a
> vector<string> to hold the results? (or, more generally, a container)

In general, the split function receives a string as input and returns an
array. (BTW, this is consistent with the majority of scripting
languages, such as Ruby, Python, etc.) Because the split function a
priori does not know in how many parts it will chop the input string,
and it is likely to be more than one, a vector fits naturally for this
task.

> Is there some technical reason why it doesn't take an output iterator
> as a parameter, so it could be called like this?
>
> std::vector<std::string> result; boost::iter_split (
> std::back_inserter<string> (result), str,
> boost::first_finder(delim)); or even: boost::iter_split (
> std::ostream_iterator<std::string>(std::cout, ", "), str,
> boost::first_finder(delim));

What you describe above is isomorphic to removing a delimiter from the
string and is a separate problem. There are separate functions for this.

   Matthias

-- 
Matthias Vallentin
vallentin_at_[hidden]
http://www.icir.org/matthias

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net