在找不到时生成默认值 [英] Generating default value when none is found

查看:168
本文介绍了在找不到时生成默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入向量,在空和3个元素之间可以有任何大小。我想要生成的字符串总是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;
}



我试过修改浮动规则,像这样: floatRule = double_ | lit(0.0f),但只是给我编译错误。同样的很多其他类似的东西,我尝试。

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 :)

编辑:只是为了清楚。如果我有一个包含2个元素的矢量:1.0和2.0,我想生成一个如下所示的字符串:1.0 2.0 0.0(最后一个值应该是默认值价值)。

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).

推荐答案

不太好,但工作

#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;
}

这篇关于在找不到时生成默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆