Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to iterate into a smaller container (i.e. stride != 1)
    primarykey
    data
    text
    <p>There is a question that is very similar in spirit <a href="https://stackoverflow.com/questions/2476425/c-stl-stdtransform-with-given-stride">here</a>. Unfortunately that question didn't prompt much response - I thought I would ask a more specific question with the hope that an alternative method can be suggested.</p> <p>I'm writing a binary file into <code>std::cin</code> (with <code>tar --to-command=./myprog</code>). The binary file happens to be a set of floats and I want to put the data into <code>std::vector&lt;float&gt;</code> - ideally the c++ way.</p> <p>I can generate a <code>std::vector&lt;char&gt;</code> very nicely (thanks to <a href="https://stackoverflow.com/questions/5420317/c-reading-and-writing-binary-file/5420568#5420568">this answer</a>)</p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; int main (int ac, char **av) { std::istream&amp; input = std::cin; std::vector&lt;char&gt; buffer; std::copy( std::istreambuf_iterator&lt;char&gt;(input), std::istreambuf_iterator&lt;char&gt;( ), std::back_inserter(buffer)); // copies all data into buffer } </code></pre> <p>I now want to transform my <code>std::vector&lt;char&gt;</code> into a <code>std::vector&lt;float&gt;</code>, presumably with <code>std::transform</code> and a function that does the conversion (a <code>char[2]</code> to a <code>float</code>, say). I am struggling however, because my <code>std::vector&lt;float&gt;</code> will have half as many elements as <code>std::vector&lt;char&gt;</code>. If I could iterate with a stride of 2 then I think I would be fine, but from the previous question it seems that I cannot do that (at least not elegantly).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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