简体   繁体   English

使用str(const char *)设置std :: stringstream的内容会产生奇怪的后果

[英]Setting the content of a std::stringstream with str(const char*) has strange consequences

I use std::stringstream extensively to generate code. 我广泛使用std::stringstream来生成代码。 I have run into some querious behavior when setting the content with the str() function followed by using the operator<< . 当使用str()函数设置内容,然后使用operator<<时,我遇到了一些奇怪的行为。 Can someone explain this behavior to me. 有人可以向我解释这种行为。 I would very much appreciate it - Thanks. 我非常感谢 - 谢谢。

Example One: 示例一:

std::stringstream ssa;
ssa.str("Setting string");
std::cout << ssa.str() << std::endl;
ssa << " adding to string";
std::cout << ssa.str() << std::endl;

Output: 输出:

Setting string 设置字符串

adding to string 添加到字符串

Expected: 预期:

Setting string 设置字符串

Setting string adding to string 设置字符串添加到字符串

So after a bit of reading up on the documentation I found that I should set the open mode to ios_base::ate : 所以在对文档进行一些阅读之后,我发现我应该将开放模式设置为ios_base::ate

std::stringstream ssb(std::ios_base::ate);
ssb.str("Setting string");
std::cout << ssb.str() << std::endl;
ssb << " adding to string";
std::cout << ssb.str() << std::endl;

Output: 输出:

Setting string 设置字符串

Setting string 设置字符串

Again (or now) I would expect: 再次(或现在)我期望:

Setting string 设置字符串

Setting string adding to string 设置字符串添加到字符串

Why am I getting the results I am, and how do I get the result I want? 为什么我会得到我的结果,我如何得到我想要的结果? Ei Being able to set the initial string of a stringstream and the append to that string ? Ei能够设置string的初始string stringstream加到该string吗?

I am aware that I can just set the content to the empty string("") and then only use the <<operator . 我知道我可以将内容设置为空string("") ,然后只使用<<operator This is in fact the workaround I am using. 这实际上是我正在使用的解决方法。 I just don't understand the observed behavior. 我只是不明白观察到的行为。 And the workaround makes my code more messy. 解决方法使我的代码更加混乱。

The default open mode for a std::stringstream object is in | out std::stringstream对象的默认打开模式是in | out in | out . in | out Using a different mode overrides these options. 使用其他模式会覆盖这些选项。 Since the stream cannot be written to, the insertion operator cannot be used to write to the stream. 由于无法写入流,因此插入操作符不能用于写入流。 This is why str() gives the same output twice. 这就是为什么str()给出相同的输出两次。

You need to manually open the stream in output mode as well: 您还需要在输出模式下手动打开流:

std::stringstream ssb("Setting string", std::ios_base::out | std::ios_base::ate);

Live Demo 现场演示

Alternatively, use an std::ostringstream , which initializes the internal std::stringbuf subobject with out | mode 或者,使用std::ostringstream ,它使用out | mode来初始化内部std::stringbuf子对象 out | mode . out | mode

std::ostringstream("Setting string", std::ios_base::ate);

Live Demo 现场演示

You forgot to set flag mode: std::ios_base::out . 你忘了设置标志模式: std::ios_base::out This will work: 这将有效:

std::stringstream ssb(std::ios_base::out | std::ios_base::ate);
ssb.str("Setting string");
std::cout << ssb.str() << std::endl;
ssb << " adding to string";
std::cout << ssb.str() << std::endl;

Output: 输出:

Setting string
Setting string adding to string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM