C++ 如何将内容附加到 stringstream 类型对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8786183/
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 append content to stringstream type object?
提问by BSalunke
i am using object of stringstream as follows:
我正在使用 stringstream 的对象,如下所示:
#include<iostream>
#include <istream>
using namespace std;
int main()
{
sting str="my.string";
std::stringstream message("HI this is firet line from initialization");
message << " second line " << " Continueing second line";
message << ", Add values: ";
message << str ;
std::cout<<"message value is :"<<message.str()<<std::endl;
return 0;
}
with above code it gives an error as follows:
使用上面的代码,它会给出如下错误:
error: variable 'std::stringstream message' has initializer but incomplete type
above error is resolved once i added the "#include " header file. but when i printed the value of message. it is getting incomplete. i.e. value i got of message is as follows:
一旦我添加了“#include”头文件,上述错误就解决了。但是当我打印消息的值时。它越来越不完整。即我得到的消息值如下:
message value is : second line Continueing second linetion , Add values: my.string
消息值为:第二行继续第二行,添加值:my.string
any suggestion on why first line is removing in output? Thanks in advance
关于为什么第一行在输出中删除的任何建议?提前致谢
回答by Vlad
stringstream
is defined in a different header:
stringstream
在不同的标题中定义:
#include <sstream>
Also, if you want the initial contents to stick you'll want something like:
此外,如果您希望初始内容保持不变,您将需要以下内容:
std::stringstream message("HI this is firet line from initialization",
ios_base::app | ios_base::out);
where app
means that every output operation writes to the end of the stream. See here.
whereapp
表示每个输出操作都写入流的末尾。见这里。
回答by Anthony Hall
Following Vlad's answer, I just tried the following (in GCC 4.7.0, mingw on Windows 7: x86_64-w64-mingw32)
按照 Vlad 的回答,我只是尝试了以下操作(在 GCC 4.7.0 中,Windows 7 上的 mingw:x86_64-w64-mingw32)
std::stringstream ss("initial string", ios_base::app | ios_base::out);
ss << " appended string";
But afterward, ss.good()
would return false immediately afterward, which was confusing, since ss.str()
was still returning the appended string I expected, "initial string appended string"
. I finally stumbled upon the following line from http://www.cplusplus.com/reference/sstream/stringstream/stringstream/:
但是之后,ss.good()
会立即返回 false ,这令人困惑,因为ss.str()
仍在返回我期望的附加字符串"initial string appended string"
. 我终于从http://www.cplusplus.com/reference/sstream/stringstream/stringstream/偶然发现了以下行:
Other values of type ios_base::openmode (such as ios_base::app) may also be specified, although whether they have an effect on stringstream objects depends on the library implementation.
也可以指定 ios_base::openmode 类型的其他值(例如 ios_base::app),尽管它们是否对 stringstream 对象有影响取决于库实现。
After reading about the "standard" ios_base::
flags, I tried this, which gave me the appended stream contents I expected, with ss.good()
returning true
:
在阅读了“标准”ios_base::
标志后,我尝试了这个,它给了我我期望的附加流内容,并ss.good()
返回true
:
std::stringstream ss("initial string", ios_base::ate | ios_base::in | ios_base::out);
[P.S. I would rather have posted this as a comment on Vlad's answer, since it's not a direct answer; it's only a comment on his suggestion for preserving previous stream contents when using operator <<
. Since I don't have enough reputation points to comment on his answer, but I think this will be useful to others who come across this, I'm posting it as an answer.]
[PS 我宁愿将此作为对弗拉德回答的评论发布,因为它不是直接的回答;这只是对他在使用operator <<
. 由于我没有足够的声望点来评论他的回答,但我认为这对遇到此问题的其他人有用,我将其发布为答案。]