设置 ostringstream 自动追加
如何为 ostringstream 对象附加数据?假设:
ostringstream oss;
oss << '0x11';
我如何设置它来执行 oss << 0x22 自动从最后一个已知元素开始?我读到我需要设置一些标志,但我该怎么做呢?
编辑:很抱歉这个问题非常含糊。可能咖啡已经快没了。从我上面的问题延伸:
ostringstream oss(osstringstream::app) 是否将其设置为每次追加?我的目标是创建一个字符串。
How do I append data for ostringstream objects? Assumingly:
ostringstream oss;
oss << '0x11';
How do I set it to perform oss << 0x22
from the last known element automatically? I read through that I need to set some flag but how do I do it?
edit: Sorry for being very very vague about the question. Prolly the coffee is wearing off. Extending from my question above:
Does ostringstream oss(osstringstream::app)
sets it to append everytime? My goal is to creating a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
未指定
app
标志传递给ostringstream
构造函数。该标志仅与ofstream
。另一方面,ostringstream
是一个流,因此,数据总是插入到先前的数据之后
插入数据,前提是在此期间没有发生寻道。毕竟,这是输出流的定义。
It is unspecified what the
app
flag does when passed to anostringstream
constructor. This flag is only relevant toofstream
. On the other hand,ostringstream
is a stream,and as such, data are always inserted after the previously
inserted data, provided no seek has occured in the meantime. This is, after all, the definition of an output stream.
当您像第一个示例一样创建
stringstream
时,它会默认追加。When you create a
stringstream
like in your first example, it will append by default.这就是
ate
和app
作为开放模式的区别。打开后将ate
位置放在最后,然后保留写入位置(所以如果你想要的话,你可以在任何地方写入),app
确保所有写入都在最后完成流的。编辑:实际上 stringstream 的行为不同,所以不要依赖它。
That's the difference between
ate
andapp
as open mode.ate
position at the end after opening and then leave the write position alone (so if you want it, you can write anywhere),app
ensure that all write is done at end of the stream.Edit: in practice behaves differently for stringstream, so don't rely on it.