|
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 hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net