Search code examples
c++vectorcopyistreamunsigned-char

Conveniently copy std::vector<unsigned char> to input stream (std::istream) object


I'm trying to make use of a function which comes in a 3rd party lib and expects an input stream object in which binary file data is transported.

Signature looks like that:

doSomething(const std::string& ...,
          const std::string& ...,
          std::istream& aData,
          const std::string& ...,
          const std::map<std::string, std::string>* ...,
          long ...,
          bool ...);

Since I can't alter/change this 3rd party lib/function, I have to adapt in "my" code. In the calling place, I have a std::vector which contains the data which is expected to be passed in an istream object. Currently I copy the vector to the stream, by iterating over it and using the << operator to copy byte by byte.

I strongly suspect that there might be a more effective/convenient way but couldn't find anything useful up to now. Any help/your thoughts are greatly appreciated.

Best, JR


Solution

  • vector<unsigned char> values;
    // ...
    
    stringstream ioss;    
    copy(values.begin(), values.end(),
         ostream_iterator<unsigned char>(ioss,","));
    
    // doSomething(a, b, ioss, d, e, f, g);