I see you are already using boost . You should try boost.spirit.qi for this purpose.
#include <vector> #include <string> #include <iostream> #include <boost/spirit/include/qi.hpp> namespace qi = ::boost::spirit::qi; template <typename Iterator> bool parse_numbers(Iterator & first, Iterator last, std::vector<double> & v) { using qi::double_; using qi::phrase_parse; using qi::_1; using boost::spirit::ascii::space; return phrase_parse(first, last, ('[' >> double_ % ',' >> ']'), space, v); } int main() { std::string s = "[ 0.0125, 2.9518e+02, 1.2833e+00, -3.5302e-04, 1.2095e+01, 1.0858e-01, 1.2112e-04, 1.1276e+03 ] # comments"; std::vector<double> v; std::string::iterator sb = s.begin(); parse_numbers(sb, s.end(), v); std::cout << "Parsed numbers:" << std::endl; for (int i = 0; i < v.size(); ++i) std::cout << v[i] << std::endl; std::cout << "Rest of line:" << std::endl; std::cout << std::string(sb, s.end()) << std::endl; }
I took the parse_numbers() function from the spirit documentation and adapted it a bit. It returns false when the parsing failed (i.e. the Unformed list), but returns true when the line has text after the list: the first iterator ( sb in main() ) will indicate where the list of numbers ended.
See the full list of documents:
http://www.boost.org/doc/libs/1_46_1/libs/spirit/doc/html/spirit/qi.html
source share