[英]How can I extract double pairs from an std::string with Boost Spirit?
我想将具有双对序列的字符串解析为带有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()用于键和值规则,并且std :: string不能从double构造。
我不确定为什么当您的输出要求是不能使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.