Search code examples
c++booststring-parsingiterator-range

Is there a function for number parsing which works on iterators?


Is there a way in C++ or Boost to parse a number (unsigned long long, if possible) which works directly on wstring iterators? It should be as fast as std::stoull.


Solution

  • Boost.Spirit has an iterator-based API. From what I've read it should be even faster than the standard string conversion functions.

    #include <iostream>
    #include <string>
    #include <boost/spirit/include/qi.hpp>
    
    int main()
    {
        namespace qi = boost::spirit::qi;
    
        std::wstring s(L"4398046511104");
    
        unsigned long long n = 0;
        qi::parse( begin(s), end(s), qi::ulong_long, n );
    
        std::cout << n << std::endl;
    }
    

    Live demo on Coliru.