How to efficiently copy istringstream?

Or ostringstream?

istringstream a("asd");
istringstream b = a; // This does not work.

      

I think memcpy won't work either.

+2


source to share


2 answers


istringstream a("asd");
istringstream b(a.str());

      

Edit: Based on your comment on another answer, it looks like you can also copy the entire contents of fstream to a strinstream. You don't want / have to do this character at the same time (and you're right - it's usually pretty slow).



// create fstream to read from
std::ifstream input("whatever");

// create stringstream to read the data into
std::istringstream buffer;

// read the whole fstream into the stringstream:
buffer << input.rdbuf();

      

+6


source


You cannot just copy streams, you need to copy your buffers using iterators. For example:



#include <sstream>
#include <algorithm>
......
std::stringstream first, second;
.....
std::istreambuf_iterator<char> begf(first), endf;
std::ostreambuf_iterator<char> begs(second);
std::copy(begf, endf, begs);

      

+2


source







All Articles