Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用boost spirit向向量添加值_C++_Boost_Boost Spirit - Fatal编程技术网

C++ 无法使用boost spirit向向量添加值

C++ 无法使用boost spirit向向量添加值,c++,boost,boost-spirit,C++,Boost,Boost Spirit,我想使用boost spirit解析以下字符串: "{"DeliverNr":7424,"fruits":[["apple","banana","orange", "raspberry"]]}" 但我只想在向量中放三个第一个水果 我有以下输出: it: { , dist: 0 谁能告诉我我做错了什么 #define BOOST_SPIRIT_USE_PHOENIX_V3 #include <boost/spirit/include/phoenix_core.hpp> #incl

我想使用boost spirit解析以下字符串:

"{"DeliverNr":7424,"fruits":[["apple","banana","orange", "raspberry"]]}"
但我只想在向量中放三个第一个水果

我有以下输出:

it: { , dist: 0
谁能告诉我我做错了什么

#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <vector>

struct SomeStructF
{
    std::string str1, str2, str3;
};

    using namespace boost::spirit;

    qi::rule<std::string::iterator> startRule;
    qi::rule<std::string::iterator> endRule;
    qi::rule<std::string::iterator, std::string()> stringRule;
    qi::rule<std::string::iterator, SomeStructF()> valueRule;
    qi::rule<std::string::iterator, std::vector<SomeStructF>()> valuesRule;
    char const QUOTATION_MARK{ '"' };
    char const OPEN_SQUARE_BRACKET{ '[' };

    startRule =
            +(qi::char_ - qi::char_(OPEN_SQUARE_BRACKET))
        >> qi::char_(OPEN_SQUARE_BRACKET);

    endRule = +qi::char_;

    stringRule =
            QUOTATION_MARK
        >> *(qi::char_ - QUOTATION_MARK)
        >> QUOTATION_MARK;

    valueRule =
            OPEN_SQUARE_BRACKET
        >> stringRule
        >> stringRule
        >> stringRule;

    valuesRule %=
            startRule
        >> valueRule
        >> endRule;

    std::vector<SomeStructF> res;
    auto it = data.begin();

    if (qi::parse(it, data.end(), valuesRule, res))
    {
        std::cout << "PARSE succeded" << std::endl;
    }
    std::cout << "it: " << *it << " , dist: " <<  std::distance(data.begin(), it) << std::endl;

    std::cout << "res size " << res.size() << std::endl;

BOOST_FUSION_ADAPT_STRUCT(
        parsers::SomeStructF,
        (std::string, str1)
        (std::string, str2)
        (std::string, str3)
)
#定义提升(精神)使用(凤凰)
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构SomeStructF
{
std::字符串str1、str2、str3;
};
使用名称空间boost::spirit;
齐:规则开始;
qi::规则endRule;
qi::规则stringRule;
qi::规则值规则;
qi::规则值规则;
字符常量引号{'''};
字符常量开方括号{'['};
startRule=
+(qi::char_uu-qi::char_u(开方括号))
>>qi::char_uu(开方括号);
endRule=+qi::char_u2;;
条规=
引号
>>*(qi::字符-引号)
>>引号;
价值法则=
开放式方括号
>>条规
>>条规
>>规则;
价值规则%=
startRule
>>价值法则
>>endRule;
std::向量res;
auto it=data.begin();
if(qi::parse(it,data.end(),valuesRule,res))
{

std::cout我不太清楚你在问什么。在我看来,很容易就可以把逗号去掉,正如你所说:

#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace qi = boost::spirit::qi;

struct SomeStructF {
    std::string str1, str2, str3;
};

BOOST_FUSION_ADAPT_STRUCT(
        SomeStructF,
        (std::string, str1)
        (std::string, str2)
        (std::string, str3)
)

int main()
{
    qi::rule<std::string::iterator> startRule;
    qi::rule<std::string::iterator> endRule;
    qi::rule<std::string::iterator, std::string()> stringRule;
    qi::rule<std::string::iterator, SomeStructF()> valueRule;
    qi::rule<std::string::iterator, SomeStructF()> valuesRule;
    char const QUOTATION_MARK{ '"' };
    char const OPEN_SQUARE_BRACKET{ '[' };

    startRule =
            +(qi::char_ - qi::char_(OPEN_SQUARE_BRACKET))
        >> qi::char_(OPEN_SQUARE_BRACKET);

    endRule = +qi::char_;

    stringRule =
            QUOTATION_MARK
        >> *(qi::char_ - QUOTATION_MARK)
        >> QUOTATION_MARK;

    valueRule =
            OPEN_SQUARE_BRACKET
        >> stringRule >> ','
        >> stringRule >> ','
        >> stringRule;

    valuesRule %=
            startRule
        >> valueRule
        >> endRule;
    BOOST_SPIRIT_DEBUG_NODES((startRule)(endRule)(stringRule)(valueRule)(valuesRule));

    std::string data = R"({"DeliverNr":7424,"fruits":
[["apple","banana","orange","raspberry"]]})";

    std::vector<SomeStructF> res;
    auto it = data.begin();

    if (qi::parse(it, data.end(), valuesRule, res))
    {
        std::cout << "PARSE succeded" << std::endl;
        std::cout << "res size " << res.size() << std::endl;

        for (auto& el : res) {
            std::cout << "el: {" << std::quoted(el.str1) << ","
                                 << std::quoted(el.str2) << ","
                                 << std::quoted(el.str3) << "}\n";
        }
    }
    else
    {
        std::cout << "Parse did not succeed\n";
    }

    if (it != data.end())
        std::cout << "Remaining unparsed: '" << std::string(it, data.end()) << "'\n";

}
using It = std::string::const_iterator;
qi::rule<It, std::string()> quoted_ = '"' >> *~qi::char_('"') >> '"';
qi::rule<It, SomeStructF()/*, qi::space_type*/> value_ = quoted_ >> ',' >> quoted_ >> ',' >> quoted_;
using boost::spirit::repository::qi::seek;
BOOST_SPIRIT_DEBUG_NODES((quoted_)(value_));

std::string const data = R"({"DeliverNr":7424,"fruits":[["apple","banana","orange","raspberry"]]}
{"DeliverNr":7435,"botanics":[["pine","maple","oak","pernambucco"]]})";

std::vector<SomeStructF> res;
It it = data.begin();

if (qi::phrase_parse(it, data.end(), *seek["[[" >> value_], qi::space, res)) {
    for (auto& el : res) {
        std::cout << "el: {" << std::quoted(el.str1) << ","
                             << std::quoted(el.str2) << ","
                             << std::quoted(el.str3) << "}\n";
    }
} else {
    std::cout << "Parse did not succeed\n";
}
您的语法(或您的问题)显示了您希望如何检测多个
SomeStructF
,因此我无法真正知道您想要什么。也许您希望简化:

#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace qi = boost::spirit::qi;

struct SomeStructF {
    std::string str1, str2, str3;
};

BOOST_FUSION_ADAPT_STRUCT(
        SomeStructF,
        (std::string, str1)
        (std::string, str2)
        (std::string, str3)
)

int main()
{
    qi::rule<std::string::iterator> startRule;
    qi::rule<std::string::iterator> endRule;
    qi::rule<std::string::iterator, std::string()> stringRule;
    qi::rule<std::string::iterator, SomeStructF()> valueRule;
    qi::rule<std::string::iterator, SomeStructF()> valuesRule;
    char const QUOTATION_MARK{ '"' };
    char const OPEN_SQUARE_BRACKET{ '[' };

    startRule =
            +(qi::char_ - qi::char_(OPEN_SQUARE_BRACKET))
        >> qi::char_(OPEN_SQUARE_BRACKET);

    endRule = +qi::char_;

    stringRule =
            QUOTATION_MARK
        >> *(qi::char_ - QUOTATION_MARK)
        >> QUOTATION_MARK;

    valueRule =
            OPEN_SQUARE_BRACKET
        >> stringRule >> ','
        >> stringRule >> ','
        >> stringRule;

    valuesRule %=
            startRule
        >> valueRule
        >> endRule;
    BOOST_SPIRIT_DEBUG_NODES((startRule)(endRule)(stringRule)(valueRule)(valuesRule));

    std::string data = R"({"DeliverNr":7424,"fruits":
[["apple","banana","orange","raspberry"]]})";

    std::vector<SomeStructF> res;
    auto it = data.begin();

    if (qi::parse(it, data.end(), valuesRule, res))
    {
        std::cout << "PARSE succeded" << std::endl;
        std::cout << "res size " << res.size() << std::endl;

        for (auto& el : res) {
            std::cout << "el: {" << std::quoted(el.str1) << ","
                                 << std::quoted(el.str2) << ","
                                 << std::quoted(el.str3) << "}\n";
        }
    }
    else
    {
        std::cout << "Parse did not succeed\n";
    }

    if (it != data.end())
        std::cout << "Remaining unparsed: '" << std::string(it, data.end()) << "'\n";

}
using It = std::string::const_iterator;
qi::rule<It, std::string()> quoted_ = '"' >> *~qi::char_('"') >> '"';
qi::rule<It, SomeStructF()/*, qi::space_type*/> value_ = quoted_ >> ',' >> quoted_ >> ',' >> quoted_;
using boost::spirit::repository::qi::seek;
BOOST_SPIRIT_DEBUG_NODES((quoted_)(value_));

std::string const data = R"({"DeliverNr":7424,"fruits":[["apple","banana","orange","raspberry"]]}
{"DeliverNr":7435,"botanics":[["pine","maple","oak","pernambucco"]]})";

std::vector<SomeStructF> res;
It it = data.begin();

if (qi::phrase_parse(it, data.end(), *seek["[[" >> value_], qi::space, res)) {
    for (auto& el : res) {
        std::cout << "el: {" << std::quoted(el.str1) << ","
                             << std::quoted(el.str2) << ","
                             << std::quoted(el.str3) << "}\n";
    }
} else {
    std::cout << "Parse did not succeed\n";
}

添加了关于如何处理多条
SomeStructF
记录的提示