3

我正在使用一个消耗成对向量的业力发生器 - 类似于http://boost-spirit.com/home/articles/karma-examples/output-generation-from-a-list-of-key-value-pairs -使用精神业力/

我按照上面的文章构建了一个示例来显示我的问题

#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/karma.hpp>

namespace karma = boost::spirit::karma;
typedef std::pair<std::string, std::string > pair_type;

template <typename OutputIterator>    
struct keys_and_values : karma::grammar<OutputIterator, std::vector<pair_type>()>
{
    keys_and_values() : keys_and_values::base_type(query)
    {
        query =  *pair;
        // here is the interesting part
        pair  =  karma::string << ' ' << karma::string << ' ' << karma::string << karma::eol;
    }
karma::rule<OutputIterator, std::vector<pair_type>()> query;
karma::rule<OutputIterator, pair_type()> pair;
};

int main(int argc, char *argv[])
{
    typedef std::back_insert_iterator<std::string> sink_type;

    std::vector<pair_type> v;
    v.push_back(pair_type("key1", "value1"));
    v.push_back(pair_type("key2", "value2"));
    v.push_back(pair_type("key3", "value3"));

    std::string generated;
    sink_type sink(generated);
    keys_and_values<sink_type> g;

    bool result = karma::generate(sink, g, v);

    std::cout << generated << std::endl;

    return 0;
}

我想要实现的是像 "value1 key1 value1" 这样的输出。通常它会输出“key1 value1”(但前提是你在我的例子中删除了第三个 karma::string)我已经尝试了很多带有语义动作的东西,例如

pair = karma::string[karma::_1 = karma::_val] ...

但是,这不起作用。我可能需要其他东西来从我的 std::pair 中获取价值。

这两个问题看起来很有趣,但没有解决我的问题 重用解析变量与 boost karma 如何访问 boost::spirit::karma 中嵌套对象的数据?

4

1 回答 1

3

尽管从技术上讲karma::duplicate[]在这里看起来很自然,但我可能会在这里使用本地:

更新:正如评论者所说,我误读并交换了键/值。在这里, BOOT_FUSION_ADAPT_STRUCT_NAMED 似乎是有序的!

BOOST_FUSION_ADAPT_STRUCT_NAMED(
    pair_type const, pair_as_vkv,
    (std::string, second)
    (std::string, first)
    (std::string, second)
)

而且,现在你可以写

template <typename OutputIterator>
struct keys_and_values : karma::grammar<OutputIterator, std::vector<pair_type>()>
{
    keys_and_values() : keys_and_values::base_type(query)
    {
        query = *pair;
        pair = karma::string << ' ' << karma::string << ' ' << karma::string << karma::eol;
    }
    karma::rule<OutputIterator, std::vector<pair_type>()> query;
    karma::rule<OutputIterator, boost::fusion::adapted::pair_as_vkv()> pair;
};

事不宜迟:看它Live On Coliru

输出

value1 key1 value1
value2 key2 value2
value3 key3 value3

完整列表

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>

typedef std::pair<std::string, std::string> pair_type;

BOOST_FUSION_ADAPT_STRUCT_NAMED(
    pair_type const, pair_as_vkv,
    (std::string, second)
    (std::string, first)
    (std::string, second)
)

namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;

template <typename OutputIterator>
struct keys_and_values : karma::grammar<OutputIterator, std::vector<pair_type>()>
{
    keys_and_values() : keys_and_values::base_type(query)
    {
        query = *pair;
        pair = karma::string << ' ' << karma::string << ' ' << karma::string << karma::eol;
    }
    karma::rule<OutputIterator, std::vector<pair_type>()> query;
    karma::rule<OutputIterator, boost::fusion::adapted::pair_as_vkv()> pair;
};

int main(int argc, char *argv[])
{
    typedef std::back_insert_iterator<std::string> sink_type;

    std::vector<pair_type> v;
    v.push_back(pair_type("key1", "value1"));
    v.push_back(pair_type("key2", "value2"));
    v.push_back(pair_type("key3", "value3"));

    std::string generated;
    sink_type sink(generated);
    keys_and_values<sink_type> g;

    karma::generate(sink, g, v);

    std::cout << generated << std::endl;

    return 0;
}
于 2014-10-27T07:56:49.617 回答