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
.
Asked
Active
Viewed 63 times
0

Felix Dombek
- 13,664
- 17
- 79
- 131
-
A simple loop will do. Set result to `0ULL`. Check if iterator references a digit. If it does, multiply result by `10`, map the digits `'0'` to `'9'` to the range `0` to `9`, and add to result. Increment iterator. Repeat until non-digit or end iterator reached. If that is done in a function, it will probably be no more than 5 lines. – Peter Apr 08 '17 at 22:53
1 Answers
1
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;
}

zett42
- 25,437
- 3
- 35
- 72