0

我想用 Boost Spirit 将带有双对序列的字符串解析为 std::map 。

我改编了来自 http://svn.boost.org/svn/boost/trunk/libs/spirit/example/qi/key_value_sequence.cpp的示例, 但我在为键和值定义正确的 qi::rule 时遇到问题:

template <typename Iterator>
struct keys_and_values : qi::grammar<Iterator, std::map<double, double> >
{
    keys_and_values()
      : keys_and_values::base_type(query)
    {
        query =  pair >> *(qi::lit(',') >> pair);
        pair  =  key >> value;

        key   =  qi::double_;
        value = +qi::double_;
    }

    qi::rule<Iterator, std::map<double, double>()>  query;
    qi::rule<Iterator, std::pair<double, double>()> pair;
    qi::rule<Iterator, std::string()>               key, value;
};

我不能将 double() 用于键和值规则,并且不能从 double 构造 std::string。

4

1 回答 1

0

我不确定为什么在输出要求为时不能使用 double() 键和值

 map<double, double>.

据我了解,下面的代码应该可以解决这个问题。

 template <typename Iterator>
 struct keys_and_values : qi::grammar<Iterator, std::map<double, double>() >
 {
     keys_and_values()
       : keys_and_values::base_type(query)
     {
         query =  pair >> *(qi::lit(',') >> pair);
         pair  =  key >> -(',' >> value);     // a pair is also separated by comma i guess

         key   =  qi::double_;
        value = qi::double_;    // note the '+' is not required here
     }

     qi::rule<Iterator, std::map<double, double>()>  query;
     qi::rule<Iterator, std::pair<double, double>()> pair;
     qi::rule<Iterator, double()>               key, value;    // 'string()' changed to 'double()'
 };

以上代码将双序列1323.323,32323.232,3232.23,32222.23的输入解析为

地图[1323.323] = 32323.232

地图[3232.23] = 32222.23

于 2010-09-08T10:50:45.970 回答