Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wraps a vector<unsigned char> inside a stream?

Tags:

c++

stl

I have a function which takes as argument an std::istream& and writes a transformed stream to an std::ostream&.

On the another hand, I have another function which accepts a vector argument.

My goal is to pass the output of the first function to the second function.

Is there something out of the box to do that ? Otherwise, how can I easily implement it ?

Thank you

Edit : here are the 2 functions signature :

functionA(std::istream& _in, std::ostream& _out);
functionB(std::vector<unsigned char>& data);

The caller would look like :

std::vector<unsigned char> data;
std::istrstream stream_in("input message");
std::ovectorstream stream_out(data); // ???
functionA(stream_in, stream_out);
functionB(stream_out.vector());
like image 780
PeeWee2201 Avatar asked Jul 17 '26 14:07

PeeWee2201


1 Answers

I think something like this might work

std::vector<unsigned char> data;
std::istringstream stream_in("input message");
std::stringstream stream_out;
functionA(stream_in, stream_out);
const std::string& str_out(stream_out.str());
copy(str_out.begin(), str_out.end(), std::back_inserter(data));
functionB(data);
like image 85
RedX Avatar answered Jul 19 '26 03:07

RedX