lundi 24 août 2015

Stream Iterators - stream of bytes

Reading files into an array of bytes can easily be accomplished using istream_iterator.

For example:

std::ifstream afile(somefile);
noskipws(afile);
...
std::vector<uint_8> vOfBytes = { std::istream_iterator<uint8_t>(afile),
                                 std::istream_iterator<uint8_t>() };

I now have a need to be able to do the same with vectors and strings. The problem is that these will very in size.

For example:

std::string astring = "abc";
std::wstring awstring = L"abc";
std::vector avector<uint32_t> = { 0, 1, 2, 3 };

// std::distance(asv.begin(), asv.end()) == 3
std::vector<uint8_t> asv = { /* astring to std::vector<uint8_t> */ }; 

// std::distance(awsv.begin(), awsv.end()) == 6
std::vector<uint8_t> awsv = { /* awstring to std::vector<uint8_t> */ };

// std::distance(uiv.begin(),uiv.end()) == 16
std::vector<uint8_t> uiv = { /* avectorto std::vector<uint8_t>*/};

I have been looking through the cpp reference for a bit and have not come across a way to treat the above as a stream of bytes w/o having to roll my own stream. Does anyone have any suggestions or references they could point me towards? I would greatly appreciate it.

Aucun commentaire:

Enregistrer un commentaire