C++ 如何重用 ostringstream?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/624260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reuse an ostringstream?
提问by twk
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?
我想清除并重用 ostringstream(和底层缓冲区),这样我的应用程序就不必进行那么多分配。如何将对象重置为其初始状态?
回答by Johannes Schaub - litb
I've used a sequence of clear and str in the past:
我过去使用过一系列 clear 和 str :
// 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 reallocations done by str
by overwriting whatever is in the output buffer currently instead. Results are like this:
这将防止str
通过覆盖当前输出缓冲区中的任何内容来完成一些重新分配。结果是这样的:
std::ostringstream s;
s << "hello";
s.seekp(0);
s << "b";
assert(s.str() == "bello");
If you want to use the string for c-functions, you can use std::ends
, putting a terminating null like this:
如果您想将字符串用于 c 函数,您可以使用std::ends
,像这样放置一个终止空值:
std::ostringstream s;
s << "hello";
s.seekp(0);
s << "b" << std::ends;
assert(s.str().size() == 5 && std::strlen(s.str().data()) == 1);
std::ends
is a relict of the deprecated std::strstream
, which was able to write directly to a char array you allocated on the stack. You had to insert a terminating null manually. However, std::ends
is not deprecated, i think because it's still useful as in the above cases.
std::ends
是 deprecated 的遗物std::strstream
,它能够直接写入您在堆栈上分配的 char 数组。您必须手动插入终止空值。但是,std::ends
并没有被弃用,我认为因为它在上述情况下仍然有用。
回答by Diego Sevilla
Seems to be that the ostr.str("")
call does the trick.
似乎是ostr.str("")
调用可以解决问题。
回答by Unkle George
If you're going to clear the buffer in a way that will cause it to be cleared before it's first use, you'll need to add something to the buffer first w/ MSVC.
如果您要以某种方式清除缓冲区,使其在第一次使用之前被清除,则需要先使用 MSVC 向缓冲区添加一些内容。
struct Foo {
std::ostringstream d_str;
Foo() {
d_str << std::ends; // Add this
}
void StrFunc(const char *);
template<class T>
inline void StrIt(const T &value) {
d_str.clear();
d_str.seekp(0); // Or else you'll get an error with this seek
d_str << value << std::ends;
StrFunc(d_str.str().c_str()); // And your string will be empty
}
};
回答by Sebastian Ganslandt
You don't. Use two differently named streams for clarity and let the optimizing compiler figure out that it can reuse the old one.
你没有。为清楚起见,使用两个不同命名的流,并让优化编译器确定它可以重用旧的流。