gpt4 book ai didi

c++ - 如何编写用于使用已知键填充 map 的语义操作?

转载 作者:太空狗 更新时间:2023-10-29 21:02:47 26 4
gpt4 key购买 nike

我正在尝试使用 boost::spirit:qi 从其他参数动态构建解析器.目标是解析字符串并填充 std::map<std::string, std::string>键和值。但是,映射的关键字段未被解析(即在生成解析器之前已知)。

我猜我需要编写一个语义操作,将 map 的键设置为适当的解析值。我可以看到 qi::_1提供解析器的内容,但如何引用返回结构(在本例中为 std::map )?

如果std::map在范围内,我可以像这样直接分配它:

parser = lit(prefix) >> value_parser[map_[key] = _1];

但在我的例子中,我想实际生成一个解析器,而不是进行解析。我猜我需要一些东西来代替 map_[key] .


提供更多上下文(根据要求):

我首先解析一个看起来像这样的"template"字符串:

/path/to/:somewhere:/nifty.json

:somewhere:旨在表示以后可以通过名称 somewhere 引用的任何字符串.我的解析器运行良好。

接下来我想从该模板生成另一个解析字符串的解析器,如下所示:

/path/to/anywhere/nifty.json

然后给我一个std::map<std::string, std::string> m其中 m["somewhere"] == "anywhere" .

最佳答案

我不确定这是否是您的想法,但继承属性可能是您的答案。您无需动态创建解析器,而是创建一个解析器,将键和对映射的引用作为您在每次调用时提供的继承属性:

// an attempt to demonstrate a parser that takes a std::map by reference and a key by value,
// then stores a parsed value into the map as the value associated with the given key

#include <string>
#include <map>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

typedef std::string::const_iterator fwd_iter_t;

namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;

typedef int value_t; // or whatever
typedef std::map<std::string, value_t> result_map_t;
// key insight - rules can take "inherited" attributes (parameters in 2nd argument):
typedef qi::rule<fwd_iter_t,
void(result_map_t&, std::string), // inherit map ref and key to use
boost::spirit::ascii::space_type> map_insert_rule_t;

int main() {

result_map_t result_map;
std::vector<std::string> keys = { "A", "B", "C" };
std::string test_data = "PREFIX 1\nPREFIX 2\nPREFIX 3";

using boost::phoenix::construct; // to create pairs
using boost::phoenix::insert; // to add pairs to the map
typedef result_map_t::value_type result_map_pair_t;
// use Phoenix actions to construct the key/value pair and insert it
map_insert_rule_t maprule = qi::lit("PREFIX")
>> qi::int_[insert(qi::_r1, // inherited map ref
construct<result_map_pair_t>(qi::_r2, qi::_1))];

fwd_iter_t beg = test_data.begin();
fwd_iter_t end = test_data.end();
for (auto k_it = keys.begin(); k_it != keys.end(); ++k_it) {
using boost::spirit::ascii::space;
if (!qi::phrase_parse(beg, end,
maprule(phoenix::ref(result_map), *k_it),
space)) {
std::cerr << "parse failed!" << std::endl;
return 1;
}
}

std::cout << "parse results:" << std::endl;
for (auto r_it = result_map.begin(); r_it != result_map.end(); ++r_it) {
std::cout << r_it->first << " " << r_it->second << std::endl;
}

return 0;
}

您可以通过继承 qi::rule 并使其成为私有(private)数据成员来从调用中消除 std::map 引用。

关于c++ - 如何编写用于使用已知键填充 map 的语义操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14840132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com