Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating default value when none is found
    primarykey
    data
    text
    <p>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:</p> <pre><code>#include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;vector&gt; #include "boost/spirit/include/karma.hpp" namespace karma = boost::spirit::karma; namespace phx = boost::phoenix; typedef std::back_insert_iterator&lt;std::string&gt; BackInsertIt; int main( int argc, char* argv[] ) { std::vector&lt;float&gt; input; input.push_back(1.0f); input.push_back(2.0f); struct TestGram : karma::grammar&lt;BackInsertIt, std::vector&lt;float&gt;(), karma::space_type&gt; { TestGram() : TestGram::base_type(output) { using namespace karma; floatRule = double_; output = repeat(3)[ floatRule ]; } karma::rule&lt;BackInsertIt, std::vector&lt;float&gt;(), karma::space_type&gt; output; karma::rule&lt;BackInsertIt, float(), karma::space_type&gt; floatRule; } testGram; std::string output; BackInsertIt sink(output); karma::generate_delimited( sink, testGram, karma::space, input ); std::cout &lt;&lt; "Generated: " &lt;&lt; output &lt;&lt; std::endl; std::cout &lt;&lt; "Press enter to exit" &lt;&lt; std::endl; std::cin.get(); return 0; } </code></pre> <p>I've tried modifying the float rule to something like this: <code>floatRule = double_ | lit(0.0f)</code>, but that only gave me compilation errors. The same for a lot of other similar stuff I tried.</p> <p>I really have no idea how to get this working. Some help would be great :)</p> <p><strong>EDIT:</strong> 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: <code>"1.0 2.0 0.0"</code> (the last value should be the default value).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload