As stated in the comments, your code is incorrect due to two str()
calls. To increase efficiency, you can avoid creating a temporary vector
, for example:
void foo(std::vector<char> &data, std::stringstream &stream) { const std::string& str = stream.str(); data.assign( str.begin(), str.end() ); }
You can also avoid the std::string
using std::istreambuf_iterator
s:
void foo(std::vector<char> &data, std::stringstream &stream) { data.assign( std::istreambuf_iterator<char>( stream ), std::istreambuf_iterator<char>() ); }
but provided that they are input iterators, vector
does not have the ability to find out how much data will be assigned, and may deteriorate a little, since reserve
cannot have enough space to avoid redistribution.
source share