0

我对 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”,我该怎么办?

4

1 回答 1

0

请注意,您的 str 是用“abc”初始化的。解析器不清除字符串,只是附加它的输出。传入一个空字符串,你会得到你想要的。

于 2013-10-08T15:29:45.190 回答