如何使用 C++字符串流附加 int?
谁能告诉我或给我一个简单的例子,说明如何将 int 附加到包含单词“Something”(或任何单词)的字符串流中?
could anyone tell me or point me to a simple example of how to append an int to a stringstream containing the word "Something" (or any word)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了将来参考,请查看此内容。
http://www.cplusplus.com/reference/iostream/stringstream/
For future reference, check this out.
http://www.cplusplus.com/reference/iostream/stringstream/
我可能会按照这个一般顺序做一些事情:
使用普通流,要添加到末尾,您可以使用
std::ios::ate
或std::ios: 打开:app
作为第二个参数,但对于字符串流,这似乎不能可靠地工作(至少对于真正的编译器来说——gcc 和 VC++ 都不会产生我期望的输出) 。I'd probably do something on this general order:
With a normal stream, to add to the end, you'd open with
std::ios::ate
orstd::ios::app
as the second parameter, but with string streams, that doesn't seem to work dependably (at least with real compilers -- neither gcc nor VC++ produces the output I'd expect when/if I do so).如果您已经在使用 boost,它具有 lexical_cast可以用于此目的。它基本上是上述版本的打包版本,适用于可以写入流和从流中读取的任何类型。
如果你还没有使用 boost,它可能不值得使用,但如果你使用了,它可以使你的代码更清晰,特别是做类似的事情
If you are already using boost, it has lexical_cast that can be be used for this. It is basically a packaged version of the above, that works on any type that can be written to and read from a stream.
Its probably not worth using if you aren't using boost already, but if you are it can make your code clearer, especially doing something like