ostringstream

How to reuse an ostringstream?

青春壹個敷衍的年華 提交于 2019-11-26 15:01:39
I'd like to clear out and reuse an ostringstream (and the underlying buffer) so that my app doesn't have to do as many allocations. How do I reset the object to its initial state? I've used a sequence of clear and str in the past: // clear, because eof or other bits may be still set. s.clear(); s.str(""); Which has done the thing for both input and output stringstreams. Alternatively, you can manually clear, then seek the appropriate sequence to the begin: s.clear(); s.seekp(0); // for outputs: seek put ptr to start s.seekg(0); // for inputs: seek get ptr to start That will prevent some

How to reuse an ostringstream?

丶灬走出姿态 提交于 2019-11-26 04:07:33
问题 I\'d like to clear out and reuse an ostringstream (and the underlying buffer) so that my app doesn\'t have to do as many allocations. How do I reset the object to its initial state? 回答1: I've used a sequence of clear and str in the past: // clear, because eof or other bits may be still set. s.clear(); s.str(""); Which has done the thing for both input and output stringstreams. Alternatively, you can manually clear, then seek the appropriate sequence to the begin: s.clear(); s.seekp(0); // for