String in int Boost-Geist mit
-
30-09-2019 - |
Frage
Ich höre, dass Geist ist wirklich schnell String int bei der Umwandlung.
Ich bin jedoch nicht in der Lage, eine einfache Funktion zu erstellen, die dies tun können. So etwas wie
int STRING_TO_INT (string & s) { / * ????? * / }
Kann jemand Verwendung boost Geist in dieser Funktion zu füllen.
Durch die Art, wie ich auf boost arbeite 1,34 und nicht die neueste Version.
Lösung
Es gibt mehrere Möglichkeiten, dies zu erreichen:
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
namespace qi = boost::spirit::qi;
std::string s("123");
int result = 0;
qi::parse(s.begin(), s.end(), qi::int_, result);
oder ein kürzerer:
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/qi_auto.hpp>
namespace qi = boost::spirit::qi;
std::string s("123");
int result = 0;
qi::parse(s.begin(), s.end(), result);
, die auf Geist auto
Funktionen basieren. Wenn Sie eine dieser in eine Funktion wickeln, bekommen Sie, was Sie wollen.
EDIT: Ich sah erst jetzt, dass Sie Boost-1.34 verwenden. So, hier ist die entsprechende Beschwörung hierfür:
#include <boost/spirit.hpp>
using namespace boost::spirit;
std::string s("123");
int result = 0;
std::string::iterator b = s.begin();
parse(b, s.end(), int_p[assign_a(result)]);
Andere Tipps
int i = boost::lexical_cast<int>(str);