I have std::stringstream
object ss1
. Now, I would like to create another copy from this one.
I try this:
std::stringstream ss2 = ss1;
or:
std::stringstream ss2(ss1)
neither works
The error message is like this:
std::ios::basic_ios(const std::ios &) is not accessible from bsl::basic_stringstream, bsl::allocator>::basic_stringstream(const bsl::basic_stringstream, bsl::allocator>&).
How to Perform Extraction or Read Operation in StringStream in C++ Like the insertion, we can also perform extraction on StringStream in C++, like the cin >> operator. We can again do this by using the >> operator or the str() function.
You can't return a stream from a function by value, because that implies you'd have to copy the stream.
Very Informally: A string is a collection of characters, a stream is a tool to manipulate moving data around. A string stream is a c++ class that lets you use a string as the source and destination of data for a stream.
The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings. By treating the strings as streams we can perform extraction and insertion operation from/to string just like cin and cout streams.
Indeed, streams are non-copyable (though they are movable).
Depending on your usage, the following works quite well:
#include <iostream> #include <sstream> int main() { std::stringstream ss1; ss1 << "some " << 123 << " stuff" << std::flush; std::stringstream ss2; ss2 << ss1.rdbuf(); // copy everything inside ss1's buffer to ss2's buffer std::cout << ss1.str() << std::endl; std::cout << ss2.str() << std::endl; }
Output:
some 123 stuff
some 123 stuff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With