<small id='919o6'></small><noframes id='919o6'>

      <i id='919o6'><tr id='919o6'><dt id='919o6'><q id='919o6'><span id='919o6'><b id='919o6'><form id='919o6'><ins id='919o6'></ins><ul id='919o6'></ul><sub id='919o6'></sub></form><legend id='919o6'></legend><bdo id='919o6'><pre id='919o6'><center id='919o6'></center></pre></bdo></b><th id='919o6'></th></span></q></dt></tr></i><div id='919o6'><tfoot id='919o6'></tfoot><dl id='919o6'><fieldset id='919o6'></fieldset></dl></div>
      <tfoot id='919o6'></tfoot>
    1. <legend id='919o6'><style id='919o6'><dir id='919o6'><q id='919o6'></q></dir></style></legend>
        <bdo id='919o6'></bdo><ul id='919o6'></ul>

        使用 boost 精神解析 int 或 double (longest_d)

        时间:2023-07-20

        <tfoot id='ElgkR'></tfoot>
          <tbody id='ElgkR'></tbody>

      1. <small id='ElgkR'></small><noframes id='ElgkR'>

        <legend id='ElgkR'><style id='ElgkR'><dir id='ElgkR'><q id='ElgkR'></q></dir></style></legend>

                <bdo id='ElgkR'></bdo><ul id='ElgkR'></ul>
              • <i id='ElgkR'><tr id='ElgkR'><dt id='ElgkR'><q id='ElgkR'><span id='ElgkR'><b id='ElgkR'><form id='ElgkR'><ins id='ElgkR'></ins><ul id='ElgkR'></ul><sub id='ElgkR'></sub></form><legend id='ElgkR'></legend><bdo id='ElgkR'><pre id='ElgkR'><center id='ElgkR'></center></pre></bdo></b><th id='ElgkR'></th></span></q></dt></tr></i><div id='ElgkR'><tfoot id='ElgkR'></tfoot><dl id='ElgkR'><fieldset id='ElgkR'></fieldset></dl></div>
                • 本文介绍了使用 boost 精神解析 int 或 double (longest_d)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找一种将字符串解析为 int 或 double 的方法,解析器应该尝试两种选择并选择匹配输入流最长部分的那个.

                  I'm looking for a way to parse a string as an int or a double, the parser should try both alternatives and choose the one matching the longest portion of the input stream.

                  有一个已弃用的指令 (longest_d) 完全符合我的要求:

                  There is a deprecated directive (longest_d) that does exactly what I'm looking for:

                  number = longest_d[ integer | real ];
                  

                  ...既然已弃用,还有其他选择吗?如果有必要实施语义操作来实现所需的行为,有人有建议吗?

                  ...since it's deprecated, there are any other alternatives? In case it's necessary to implement a semantic action to achieve the desired behavior, does anyone have a suggestion?

                  推荐答案

                  首先,切换到 Spirit V2 - 它已经取代了经典精神多年.

                  Firstly, do switch to Spirit V2 - which has superseded classical spirit for years now.

                  其次,您需要确保首选 int.默认情况下,double 可以同样好地解析任何整数,因此您需要使用 strict_real_policies 代替:

                  Second, you need to make sure an int gets preferred. By default, a double can parse any integer equally well, so you need to use strict_real_policies instead:

                  real_parser<double, strict_real_policies<double>> strict_double;
                  

                  现在您可以简单地声明

                  number = strict_double | int_;
                  

                  • realpolicies 文档

                  查看测试程序在 Coliru 上直播

                  See test program Live on Coliru

                  #include <boost/spirit/include/qi.hpp>
                  
                  using namespace boost::spirit::qi;
                  
                  using A  = boost::variant<int, double>;
                  static real_parser<double, strict_real_policies<double>> const strict_double;
                  
                  A parse(std::string const& s)
                  {
                      typedef std::string::const_iterator It;
                      It f(begin(s)), l(end(s));
                      static rule<It, A()> const p = strict_double | int_;
                  
                      A a;
                      assert(parse(f,l,p,a));
                  
                      return a;
                  }
                  
                  int main()
                  {
                      assert(0 == parse("42").which());
                      assert(0 == parse("-42").which());
                      assert(0 == parse("+42").which());
                  
                      assert(1 == parse("42.").which());
                      assert(1 == parse("0.").which());
                      assert(1 == parse(".0").which());
                      assert(1 == parse("0.0").which());
                      assert(1 == parse("1e1").which());
                      assert(1 == parse("1e+1").which());
                      assert(1 == parse("1e-1").which());
                      assert(1 == parse("-1e1").which());
                      assert(1 == parse("-1e+1").which());
                      assert(1 == parse("-1e-1").which());
                  }
                  

                  这篇关于使用 boost 精神解析 int 或 double (longest_d)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:BOOST ASIO - 如何编写控制台服务器 下一篇:使用 C++ 和 BOOST 读取 JSON 文件

                  相关文章

                  <i id='Y4SO5'><tr id='Y4SO5'><dt id='Y4SO5'><q id='Y4SO5'><span id='Y4SO5'><b id='Y4SO5'><form id='Y4SO5'><ins id='Y4SO5'></ins><ul id='Y4SO5'></ul><sub id='Y4SO5'></sub></form><legend id='Y4SO5'></legend><bdo id='Y4SO5'><pre id='Y4SO5'><center id='Y4SO5'></center></pre></bdo></b><th id='Y4SO5'></th></span></q></dt></tr></i><div id='Y4SO5'><tfoot id='Y4SO5'></tfoot><dl id='Y4SO5'><fieldset id='Y4SO5'></fieldset></dl></div>

                • <small id='Y4SO5'></small><noframes id='Y4SO5'>

                      <legend id='Y4SO5'><style id='Y4SO5'><dir id='Y4SO5'><q id='Y4SO5'></q></dir></style></legend>
                    1. <tfoot id='Y4SO5'></tfoot>

                      • <bdo id='Y4SO5'></bdo><ul id='Y4SO5'></ul>