2010-09-08 47 views
0

我想解析一个双精度序列的字符串到具有Boost Spirit的std :: map 中。如何使用Boost Spirit从std :: string中提取双对?

我改编自 http://svn.boost.org/svn/boost/trunk/libs/spirit/example/qi/key_value_sequence.cpp 的例子,但我有difining适当补气::规则键和值的一个问题:

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; 
}; 

我不能双用()为键和值规则和std :: string不能从012构造。

回答

0

我不知道你为什么不能双用()键和值当你的输出要求是

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

+0

是的,这作品。谢谢! – 2010-09-08 19:14:48

相关问题