Clearing std::stringstream

W

Woodster

I am using std::stringstream to format a string. How can I clear the
stringstream variable I am using to "re use" the same variable?

Eg:

Using std::string

std::string buffer;

buffer = "value1" + " : " + "value2";
buffer = "value3" + " : " + "value4";

Gives values of
value1 : value2
value3 : value4

The = operator for std::string overwrites the preivous contents of the
string, but I have been unable to locate a similar thing for
std:stringstream

using std::stringstream

std::stringstream buffer;

buffer << "value1" << " : " << "value2";
buffer << "value3" << " : " << "value4";

Gives values of
value1 : value2
value1 : value2value3 : value4

Which is not the desired result. How should I go about clearing the
buffer of a std::stringstream?

Thanks in advance
 
G

Gianni Mariani

Woodster wrote:
....
using std::stringstream

std::stringstream buffer;

buffer << "value1" << " : " << "value2";

buffer.str( "" ); // set the contents to the empty string.
buffer << "value3" << " : " << "value4";

Gives values of
value1 : value2
value1 : value2value3 : value4

Which is not the desired result. How should I go about clearing the
buffer of a std::stringstream?

use the .str( string ) method ... it should be an FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top