There is a question that is very similar in spirit here. 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.
I'm writing a binary file into std::cin
(with tar --to-command=./myprog
).
The binary file happens to be a set of floats and I want to put the data into std::vector<float>
- ideally the c++ way.
I can generate a std::vector<char>
very nicely (thanks to this answer)
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
int
main (int ac, char **av)
{
std::istream& input = std::cin;
std::vector<char> buffer;
std::copy(
std::istreambuf_iterator<char>(input),
std::istreambuf_iterator<char>( ),
std::back_inserter(buffer)); // copies all data into buffer
}
I now want to transform my std::vector<char>
into a std::vector<float>
, presumably with std::transform
and a function that does the conversion (a char[2]
to a float
, say). I am struggling however, because my std::vector<float>
will have half as many elements as std::vector<char>
. 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).