anon
anon

Reputation:

Best way to empty stringstream?

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?

Upvotes: 41

Views: 46067

Answers (1)

Brian R. Bondy
Brian R. Bondy

Reputation: 347276

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.

Upvotes: 51

Related Questions