views:

495

answers:

1

One of the possibilities is:

somestringstream.str("");

But is it most optimal? Is there any way to preserve stringstream internal buffer, so that following operator<<() calls would not require to reserve memory again?

+3  A: 

I've always done:

s.clear();//clear any bits set
s.str(std::string());

@litb gets into more detail about how to seekp to the start of the stream combined with std::ends you can keep your allocated size.

Brian R. Bondy
or simply ss.str(""); //more intuitive
milan1612
@milan1612: You would still need to clear the bits, and sending a C-style char* will be slower.
Shmoopty