I can recommend Boost Spirit (disassemble with Qi):
- some landmarks
- See also my other atoi answer in a character array with lots of integers
- several examples are used:
.
#include <boost/spirit/include/qi.hpp> namespace qi = boost::spirit::qi; const char *demo1 = "1234"; const char *demo2 = "1234,2345,-777,-888"; const char *demo3 = " 1234 , 2345 , -777, -888 "; void do_demo1() { const char *begin = demo1; const char *iter = begin; const char *end = demo1+strlen(demo1); int result; if (qi::parse(iter, end, qi::int_, result)) std::cout << "result = " << result << std::endl; else std::cout << "parse failed at #" << (iter - begin) << ": " << std::string(iter, end) << std::endl; //// to allow for spaces, use phrase_parse instead of parse // if (qi::phrase_parse(begin, end, qi::int_, qi::space, result) //// ... etc } void do_demo2() { const char *begin = demo2; const char *iter = begin; const char *end = demo2+strlen(demo2); std::vector<int> results; if (qi::parse(iter, end, qi::int_ % ',', results)) std::cout << "results = " << results.size() << std::endl; else std::cout << "parse failed at #" << (iter - begin) << ": " << std::string(iter, end) << std::endl; } void do_demo3() { const char *begin = demo3; const char *iter = begin; const char *end = demo3+strlen(demo3); std::vector<int> results; if (qi::phrase_parse(iter, end, qi::int_ % ',', qi::space, results)) std::cout << "results = " << results.size() << std::endl; else std::cout << "parse failed at #" << (iter - begin) << ": " << std::string(iter, end) << std::endl; } int main() { do_demo1(); do_demo2(); do_demo3(); return 0; }
Other
Be sure to look at the binary (de) serialization IFF, which you can dictate the format of the stream (text). See my recent answer here for a comparison of methods when it is intended for serialization / deserialization:
- STL (ANSI C ++ 98 Standard Library)
- Boost Spirit (above)
- Update sequence
This publication contains benchmarks
sehe Jun 19 '11 at 18:30 2011-06-19 18:30
source share