2010-09-08 39 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

相關問題