For algorithms that only use an InputIterator or ForwardIterator for the input, a simple cast is sufficient. For more complex algorithms writing a wrapper, writing a specialized iterator, or using Boost functionality may be necessary. Provided the algorithm input aligns with those conditions, something like this will work:
ofstream foo("foo.txt", ios_base::binary);
vector<int> bar = {13, 42};
copy(reinterpret_cast<const char*>(&*bar.cbegin()), reinterpret_cast<const char*>(&*bar.cend()), ostreambuf_iterator(foo));
Obviously this needs to be round trip certified before it can be considered dependable. Validating that the values in output
are consecutive can be tedious so code was hijacked from here to do that:
ofstream foo("foo.txt", ios::binary);
vector<int> bar(numeric_limits<unsigned char>::max() + 1);
iota(bar.begin(), bar.end(), 0);
copy(reinterpret_cast<const char*>(&*bar.data()), reinterpret_cast<const char*>(&*bar.data() + bar.size()), ostreambuf_iterator<char>(foo));
foo.close();
ifstream file_read("foo.txt", ios::binary);
vector<decltype(bar)::value_type> output(bar.size());
copy(istreambuf_iterator<char>(file_read), istreambuf_iterator<char>(), reinterpret_cast<char*>(&*output.data()));
cout << "First element: " << output.front() << "\nLast element: " << output.back() << "\nAny non-consecutive elements: " << (output.cend() == mismatch(output.cbegin(), prev(output.cend()), next(output.cbegin()), [](auto first1, auto first2) { return first1 + 1 == first2; }).second ? "no\n" : "yes\n");
The output from this demonstrates that this method is actually successful:
First element: 0
Last element: 255
Any non-consecutive elements: no
Although not every possible int
was tried, every possible char
was tried, and since any int
can be made up of a collection of char
s this demonstrates that any collection of int
s is streamable this way.