我有以下输入123, test, test456
并且我想运行boost :: qi语法,以便输出是成对的向量,其中每个匹配与某些类型的信息相关联,例如:
[(123, Int), (test, String), (test456, String)]
这是我到目前为止所尝试的内容:
enum class MatchType
{
Int,
String
}
在语法中
match = qi::alpha >> *(qi::alnum)[/*How to set the tuple to (matched value _1, string)*/]
| +qi::digit[ /*How to set the tuple to (matched value _1, Int)*/ ]
/* Tied but doesn't compile:
+qi::digit[
[]()
{
phoenix::at_c<0>(_val) = _1;
phoenix::at_c<1>(_val) = MatchType::Int;
}]
*/
qi::rule<Iterator, std::vector<std::pair<MatchType, std::string>>> match;
实现这一目标的最佳方法是什么?
答案 0 :(得分:3)
我只是建议
#include <boost/fusion/adapted/std_pair.hpp>
以后
my_pair = (qi::attr(MatchType::Int) >> qi::int_)
| (qi::attr(MatchType::String) >> +qi::alnum);