如何从 qi::double_ 转换为字符串?
How can I convert from qi::double_ to string?
我正在使用 spiri::qi
来解析文本并将我解析的内容推送到 vector<string>
,大部分情况下它很好,因为它们主要是名称和地址,但也有一些数字我正在用 double_
解析,但是一旦我将它推送到 vector
,它就会认为它是一个字符代码,例如 '\x3'
代替 3.0
。我不想使用 variant
,因为它在少数情况下工作量太大。无论如何,我可以在推送之前将 double_
的结果转换为 string
吗?
如果您的问题只是:
Anyway I can convert the result of double_
to string
before pushing it?
然后是的,您可以使用 to_string
.
将任何数字转换回 string
使用 raw[double_]
(或者更确切地说 as_string[raw[double_]]
)。
第一个与具有 属性与字符容器 兼容的任何规则一样工作。后者自动公开一个 std::string
(std::wstring
也有一个)。
奖金
要完成 Jonathan Mee 对 "just use the language" 的建议(释义...:))你 可以 ,请参阅下面的最后一个演示语法:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
int main() {
using It = std::string::const_iterator;
auto to_string_f = [](auto v) { return std::to_string(v); };
px::function<decltype(to_string_f)> to_string_ { to_string_f };
for (std::string const input : { "3.14", "+inf", "NaN", "-INF", "99e-3" })
{
for (auto const& grammar : std::vector<qi::rule<It, std::string()>> {
//qi::double_, // results in strange binary interpretations, indeed
qi::raw[ qi::double_ ],
qi::as_string [ qi::raw[ qi::double_ ] ],
qi::double_ [ qi::_val = to_string_(qi::_1) ],
})
{
auto f = input.begin(), l = input.end();
std::string result;
if (qi::parse(f, l, grammar, result))
std::cout << input << "\t->\t" << result << "\n";
else
std::cout << "FAILED for '" << input << "'\n";
}
}
}
打印
3.14 -> 3.14
3.14 -> 3.14
3.14 -> 3.140000
+inf -> +inf
+inf -> +inf
+inf -> inf
NaN -> NaN
NaN -> NaN
NaN -> nan
-INF -> -INF
-INF -> -INF
-INF -> -inf
99e-3 -> 99e-3
99e-3 -> 99e-3
99e-3 -> 0.099000
请注意,std::to_string
不会产生文字输入,因此它可能无法忠实地满足您的目的。
我正在使用 spiri::qi
来解析文本并将我解析的内容推送到 vector<string>
,大部分情况下它很好,因为它们主要是名称和地址,但也有一些数字我正在用 double_
解析,但是一旦我将它推送到 vector
,它就会认为它是一个字符代码,例如 '\x3'
代替 3.0
。我不想使用 variant
,因为它在少数情况下工作量太大。无论如何,我可以在推送之前将 double_
的结果转换为 string
吗?
如果您的问题只是:
Anyway I can convert the result of
double_
tostring
before pushing it?
然后是的,您可以使用 to_string
.
string
使用 raw[double_]
(或者更确切地说 as_string[raw[double_]]
)。
第一个与具有 属性与字符容器 兼容的任何规则一样工作。后者自动公开一个 std::string
(std::wstring
也有一个)。
奖金
要完成 Jonathan Mee 对 "just use the language" 的建议(释义...:))你 可以 ,请参阅下面的最后一个演示语法:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
int main() {
using It = std::string::const_iterator;
auto to_string_f = [](auto v) { return std::to_string(v); };
px::function<decltype(to_string_f)> to_string_ { to_string_f };
for (std::string const input : { "3.14", "+inf", "NaN", "-INF", "99e-3" })
{
for (auto const& grammar : std::vector<qi::rule<It, std::string()>> {
//qi::double_, // results in strange binary interpretations, indeed
qi::raw[ qi::double_ ],
qi::as_string [ qi::raw[ qi::double_ ] ],
qi::double_ [ qi::_val = to_string_(qi::_1) ],
})
{
auto f = input.begin(), l = input.end();
std::string result;
if (qi::parse(f, l, grammar, result))
std::cout << input << "\t->\t" << result << "\n";
else
std::cout << "FAILED for '" << input << "'\n";
}
}
}
打印
3.14 -> 3.14
3.14 -> 3.14
3.14 -> 3.140000
+inf -> +inf
+inf -> +inf
+inf -> inf
NaN -> NaN
NaN -> NaN
NaN -> nan
-INF -> -INF
-INF -> -INF
-INF -> -inf
99e-3 -> 99e-3
99e-3 -> 99e-3
99e-3 -> 0.099000
请注意,std::to_string
不会产生文字输入,因此它可能无法忠实地满足您的目的。