我聽說靈魂在將字符串轉換爲int時真的很快。使用提升精神的字符串轉換爲int
但是,我無法創建一個簡單的功能,可以這樣做。像
INT string_to_int(字符串& S){ /* * ?????/ }
任何人都可以使用升壓精神的東西來填補這個功能。
順便說一句,我正在努力提高1.34而不是最新版本。
我聽說靈魂在將字符串轉換爲int時真的很快。使用提升精神的字符串轉換爲int
但是,我無法創建一個簡單的功能,可以這樣做。像
INT string_to_int(字符串& S){ /* * ?????/ }
任何人都可以使用升壓精神的東西來填補這個功能。
順便說一句,我正在努力提高1.34而不是最新版本。
有幾種方法來實現這一目標:
#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);
或較短:
#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);
這是基於精神的auto
功能。如果你將其中的一個包裝到一個函數中,你會得到你想要的。
編輯:我只看到你現在使用升壓1.34。所以這裏是相應的咒語:
#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)]);
謝謝。我在我的配置文件中看到,它比atoi快大約2.5倍。 – rahul 2010-10-17 08:10:05
至少在Boost 1.58中,您必須更改包含才能編譯它。 '解析'爲'qi_parse'。而且你還必須添加'qi_auto'來編譯較短的版本。 – 2015-06-06 23:48:24
int i = boost::lexical_cast<int>(str);
我已經閱讀過幾次boost :: lexical cast對於這種微不足道的轉換真的很慢。另見http://stackoverflow.com/questions/1250795/very-poor-boostlexical-cast-performance – Ralf 2010-10-15 08:56:28
它不是「真的」快,它只是運行在一個正常的速度。曾聽說過這種說法,在盲人的土地上,獨眼人是王嗎? – 2011-01-02 02:59:27