我对boost.spirit.qi字符串解析器有疑问。当我想要解析字符串值到std :: string属性时,如下所示:
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/assert.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
template <typename P, typename T>
void test_phrase_parser_attr(
char const* input, P const& p, T& attr, bool full_match = true)
{
using boost::spirit::qi::phrase_parse;
using boost::spirit::qi::ascii::space;
char const* f(input);
char const* l(f + strlen(f));
if (phrase_parse(f, l, p, space, attr) && (!full_match || (f == l)))
std::cout << "ok" << std::endl;
else
std::cout << "fail" << std::endl;
}
int main()
{
std::string str("abc");
test_phrase_parser_attr("cba", string("cba"), str);
std::cout << str << std::endl;
return 0;
}
输出:abccba
但我希望程序输出“cba”,我该怎么办?
答案 0 :(得分:0)
请注意,您的str已使用“abc”初始化。解析器不会清除字符串,只是附加其输出。 传入一个空字符串,你会得到你想要的东西。