3

I have an input vector that can have any size between empty and 3 elements. I want the generated string to always be 3 floats separated by spaces, where a default value is used if there aren't enough elements in the vector. So far I've managed to output only the contents of the vector:

#include <iostream>
#include <iterator>
#include <vector>

#include "boost/spirit/include/karma.hpp"

namespace karma = boost::spirit::karma;
namespace phx   = boost::phoenix;
typedef std::back_insert_iterator<std::string> BackInsertIt;

int main( int argc, char* argv[] )
{
    std::vector<float> input;
    input.push_back(1.0f);
    input.push_back(2.0f);

    struct TestGram 
        : karma::grammar<BackInsertIt, std::vector<float>(), karma::space_type>
    {
        TestGram() : TestGram::base_type(output)
        {
            using namespace karma;
            floatRule = double_;

            output = repeat(3)[ floatRule ];
        }

        karma::rule<BackInsertIt, std::vector<float>(), karma::space_type> output;
        karma::rule<BackInsertIt, float(), karma::space_type> floatRule;
    } testGram;


    std::string output;
    BackInsertIt sink(output);
    karma::generate_delimited( sink, testGram, karma::space, input );

    std::cout << "Generated: " << output << std::endl;

    std::cout << "Press enter to exit" << std::endl;
    std::cin.get();
    return 0;
}

I've tried modifying the float rule to something like this: floatRule = double_ | lit(0.0f), but that only gave me compilation errors. The same for a lot of other similar stuff I tried.

I really have no idea how to get this working. Some help would be great :)

EDIT: Just to make it clear. If I have a vector containing 2 elements: 1.0 and 2.0, I want to generate a string that looks like this: "1.0 2.0 0.0" (the last value should be the default value).

4

2 回答 2

2

不漂亮,但工作:

#include <iostream>
#include <iterator>
#include <vector>
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include "boost/spirit/include/karma.hpp"
#include <boost/spirit/include/phoenix.hpp>

namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
typedef std::back_insert_iterator<std::string> BackInsertIt;

int main(int argc, char* argv[]) {
  std::vector<float> input;
  input.push_back(1.0f);
  input.push_back(2.0f);

  struct TestGram: karma::grammar<BackInsertIt, std::vector<float>(),
      karma::space_type> {
    TestGram()
        : TestGram::base_type(output) {
      using namespace karma;
      floatRule = double_;

      output = repeat(phx::bind(&std::vector<float>::size, (karma::_val)))[floatRule]
            << repeat(3 - phx::bind(&std::vector<float>::size, (karma::_val)))[karma::lit("0.0")];
    }

    karma::rule<BackInsertIt, std::vector<float>(), karma::space_type> output;
    karma::rule<BackInsertIt, float(), karma::space_type> floatRule;
  } testGram;

  std::string output;
  BackInsertIt sink(output);
  karma::generate_delimited(sink, testGram, karma::space, input);

  std::cout << "Generated: " << output << std::endl;

  return 0;
}
于 2013-09-02T20:31:56.320 回答
1

大警告:

显示的代码有缺陷,要么是由于错误,要么是由于滥用业力属性传播(见评论)。

它调用未定义的行为(可能)取消引用end()输入向量上的迭代器。

这应该工作

    floatRule = double_ | "0.0";

    output = -floatRule << -floatRule << -floatRule;

注意,floatRule应该接受一个optional<float>代替。在 Coliru 上看到它

最小的例子:

#include "boost/spirit/include/karma.hpp"

namespace karma = boost::spirit::karma;
using It = boost::spirit::ostream_iterator;

int main( int argc, char* argv[] )
{
    const std::vector<float> input { 1.0f, 2.0f };

    using namespace karma;
    rule<It, boost::optional<float>()> floatRule      = double_ | "0.0";
    rule<It, std::vector<float>(), space_type> output = -floatRule << -floatRule << -floatRule;

    std::cout << format_delimited(output, space, input);
}

于 2013-09-03T11:31:54.900 回答