c++ - Stream Iterators - stream of bytes -


reading files array of bytes can 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 have need able same vectors , strings. problem these 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 std::vector<uint8_t> */ };   // std::distance(awsv.begin(), awsv.end()) == 6 std::vector<uint8_t> awsv = { /* awstring 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 cpp reference bit , have not come across way treat above stream of bytes w/o having roll own stream. have suggestions or references point me towards? appreciate it.

you can use same iterator constructor

#include <string> #include <vector> #include <iostream>  int main() {   std::string astring = "abc";   std::wstring awstring = l"abc";   std::vector<uint32_t> avector{ 0, 1, 2, 3 };    std::vector<uint8_t> asv{     reinterpret_cast<uint8_t const*>(astring.data()),     reinterpret_cast<uint8_t const*>(astring.data() + astring.size()),   };     std::vector<uint8_t> awsv{     reinterpret_cast<uint8_t const*>(awstring.data()),     reinterpret_cast<uint8_t const*>(awstring.data() + awstring.size()),   };     std::vector<uint8_t> uiv{     reinterpret_cast<uint8_t const*>(avector.data()),     reinterpret_cast<uint8_t const*>(avector.data() + avector.size()),   };    std::cout << std::hex;    (auto v : asv)     std::cout << static_cast<int>(v) << ' ';   std::cout << '\n';   (auto v : awsv)     std::cout << static_cast<int>(v) << ' ';   std::cout << '\n';   (auto v : uiv)     std::cout << static_cast<int>(v) << ' ';   std::cout << '\n'; } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -