Boost logo

Boost Users :

Subject: Re: [Boost-users] RunTime error + lexical_cast + string with space
From: Igor R (boost.lists_at_[hidden])
Date: 2011-12-05 07:49:56

> I noticed the below crashes[run time error because of space in string]
> Is there a possibility to handle this situation? [without explicitly
> calling split]
>   string s = "   12252001";
>   int offsets[] = {5,2,4};
>   offset_separator f(offsets, offsets+3);
>   tokenizer<offset_separator> tok(s,f);
>   for(tokenizer<offset_separator>::iterator beg=tok.begin();
> beg!=tok.end();++beg)
>           int val = boost::lexical_cast<int>( *beg);

You should explicitly trim every token (use boost/algorithm/string/trim.hpp).
Alternatively, you could use more flexible facility, like Spirit.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at