Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-09-08 03:04:24


Pavol Droba wrote:

>> suppose I have a string "module.foo1.port" and want to get the second
>> dot-separated element. I think I can use the 'split' algorithm, but it
>> does not look very convenient, I need to declare container, then call
>> split and then obtain the result.
>>
>
> You can also use split_iterator.
>
> #include <boost/algorithm/string/find_iterator.hpp>
> #include <boost/algorithm/string/finder.hpp>
>
> using namespace boost;
>
> std::string str="module.foo1.port";
> typedef split_iterator<std::string::iterator> string_split;
>
> string_split it(str, token_finder(is_any_of(".")); // *it="module"
> ++it; // *it="foo1"
> ++it; // *it="port"
> ++it; // it.eof()==true it=string_split();
>
>
> *it is an iterator_range, pointing to the input. You can easily convert it
> to a string
>
> std::string match=copy_iterator_range<std::string>(*it);

Ok, noted. Though what I have now:

  vector<string> parts;
  split(parts, p.options[i].string_key, is_any_of("."));
  if (parts.size() > 2)
      modules.insert(parts[1]);

has roughly the same size.

BTW, looking at

http://www.boost.org/regression-logs/cs-win32_metacomm/doc/html/class.boost.algorithm.split_iterator.html

I don't see any explanation what's FinderT. Maybe the phrase

   Split iterator encapsulates a Finder

should include a link to the definition of the 'Finder' concept? Also, the
name 'token_finder' is a bit misleading. I associate it with item returned
by the lexer, which can have several characters. From the docs it seems
that the 'token_finder' searches for a single character, so maybe it should
be 'char_finder'? It looks like the word 'token' is used in just a couple
of places.

- Volodya


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk