ostringstream s; s << "123"; cout << s.str().c_str() << endl; // how to clear ostringstream here? s << "456"; cout << s.str().c_str() << endl;
Output is:
123 123456
I need:
123 456
How can I reset ostringstream to get desired output?
For best results, spray the surface of the metal with several sprays of vinegar, and then pull out another clean microfiber cloth to wipe the metal. The vinegar will clean the surface and remove all traces of other compounds and cleaners. Once the stainless steel is clean and dry, start polishing.
You can easily clear the content of a StringStream object by using the predefined ss. clear() function. The function will erase the data in the buffer and make the object empty. The below code demonstrates how to clear StringStream in C++.
s.str(""); s.clear();
The first line is required to reset the string to be empty; the second line is required to clear any error flags that may be set. If you know that no error flags are set or you don't care about resetting them, then you don't need to call clear()
.
Usually it is easier, cleaner, and more straightforward (straightforwarder?) just to use a new std::ostringstream
object instead of reusing an existing one, unless the code is used in a known performance hot spot.
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