Best way to empty stringstream?

一笑奈何 提交于 2019-11-27 13:26:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/834622/best-way-to-empty-stringstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!