C++ istringstream、ostringstream 和 stringstream 之间有什么区别?/ 为什么不在每种情况下都使用 stringstream?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3292107/
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
What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?
提问by Oliver Baur
When would I use std::istringstream
, std::ostringstream
and std::stringstream
and why shouldn't I just use std::stringstream
in every scenario (are there any runtime performance issues?).
当我会用std::istringstream
,std::ostringstream
以及std::stringstream
为什么不应该我只是用std::stringstream
在所有情况(是否有任何的运行时性能问题?)。
Lastly, is there anything bad about this (instead of using a stream at all):
最后,这有什么不好的吗(而不是使用流):
std::string stHehe("Hello ");
stHehe += "stackoverflow.com";
stHehe += "!";
采纳答案by CB Bailey
Personally, I find it very rare that I want to perform streaming into and out of the same string stream.
就我个人而言,我发现我很少想在同一个字符串流中执行流式传输。
Usually I want to either initialize a stream from a string and then parse it; or stream things to a string stream and then extract the result and store it.
通常我想从一个字符串初始化一个流然后解析它;或将事物流式传输到字符串流,然后提取结果并存储它。
If you're streaming to and from the same stream, you have to be very careful with the stream state and stream positions.
如果您在同一个流之间进行流式传输,则必须非常小心流状态和流位置。
Using 'just' istringstream
or ostringstream
better expresses your intent and gives you some checking against silly mistakes such as accidental use of <<
vs >>
.
使用 'just'istringstream
或ostringstream
更好地表达您的意图,并为您检查一些愚蠢的错误,例如意外使用<<
vs >>
。
There mightbe some performance improvement but I wouldn't be looking at that first.
有可能会有一些性能改进,但我不会在第一次看。
There's nothing wrong with what you've written. If you find it doesn't perform well enough, then you could profile other approaches, otherwise stick with what's clearest. Personally, I'd just go for:
你写的没有任何问题。如果您发现它表现得不够好,那么您可以分析其他方法,否则坚持使用最清晰的方法。就个人而言,我只想:
std::string stHehe( "Hello stackoverflow.com!" );
回答by Jerry Coffin
A stringstream
is somewhat larger, and might have slightly lower performance -- multiple inheritance can require an adjustment to the vtable pointer. The main difference is (at least in theory) better expressing your intent, and preventing you from accidentally using >>
where you intended <<
(or vice versa). OTOH, the difference is sufficiently small that especially for quick bits of demonstration code and such, I'm lazy and just use stringstream
. I can't quite remember the last time I accidentally used <<
when I intended >>
, so to me that bit of safety seems mostly theoretical (especially since if you domake such a mistake, it'll almost always be reallyobvious almost immediately).
Astringstream
稍大一些,并且性能可能稍低——多重继承可能需要对 vtable 指针进行调整。主要区别是(至少在理论上)更好地表达您的意图,并防止您意外使用>>
您想要的地方<<
(反之亦然)。OTOH,差异足够小,特别是对于演示代码等的快速位,我很懒惰,只使用stringstream
. 我不太记得了,我不小心用最后一次<<
,当我打算>>
,所以对我的安全是位似乎大多是理论(尤其是因为如果你这样做犯这样的错误,它会几乎总是真的很明显几乎立即)。
Nothing at all wrong with just using a string, as long as it accomplishes what you want. If you're just putting strings together, it's easy and works fine. If you want to format other kinds of data though, a stringstream
will support that, and a string mostly won't.
只使用字符串没有任何问题,只要它能完成你想要的。如果你只是把字符串放在一起,这很容易而且工作正常。如果你想格式化其他类型的数据,astringstream
会支持,而字符串大多不会。
回答by Mark B
In most cases, you won't find yourself needing both input and output on the same stringstream, so using std::ostringstream
and std::istringstream
explicitly makes your intention clear. It also prevents you from accidentally typing the wrong operator (<<
vs >>
).
在大多数情况下,您不会发现自己需要在同一个字符串流上同时输入和输出,因此使用std::ostringstream
和std::istringstream
明确地使您的意图变得清晰。它还可以防止您不小心输入错误的运算符 ( <<
vs >>
)。
When you need to do both operations on the same stream you would obviously use the general purpose version.
当您需要在同一个流上执行这两个操作时,您显然会使用通用版本。
Performance issues would be the least of your concerns here, clarity is the main advantage.
性能问题在这里是您最不关心的问题,清晰度是主要优势。
Finally there's nothing wrong with using string append as you have to construct pure strings. You just can't use that to combine numbers like you can in languages such as perl.
最后,使用字符串追加没有任何问题,因为您必须构造纯字符串。您只是不能像在 perl 之类的语言中那样使用它来组合数字。
回答by Scharron
istringstream is for input, ostringstream for output. stringstream is input and output. You can use stringstream pretty much everywhere. However, if you give your object to another user, and it uses operator >> whereas you where waiting a write only object, you will not be happy ;-)
istringstream 用于输入,ostringstream 用于输出。stringstream 是输入和输出。您几乎可以在任何地方使用 stringstream。但是,如果您将对象提供给另一个用户,并且它使用运算符 >> 而您在等待只写对象,您将不会高兴;-)
PS: nothing bad about it, just performance issues.
PS:没什么不好的,只是性能问题。
回答by David Thornley
To answer your third question: No, that's perfectly reasonable. The advantage of using streams is that you can enter any sort of value that's got an operator<<
defined, while you can only add strings (either C++ or C) to a std::string
.
回答你的第三个问题:不,这是完全合理的。使用流的优点是您可以输入任何类型的已operator<<
定义值,而您只能将字符串(C++ 或 C)添加到std::string
.
回答by Amardeep AC9MF
Presumably when only insertion or only extraction is appropriate for your operation you could use one of the 'i' or 'o' prefixed versions to exclude the unwanted operation.
大概当只有插入或仅提取适合您的操作时,您可以使用 'i' 或 'o' 前缀版本之一来排除不需要的操作。
If that is not important then you can use the i/o version.
如果这不重要,那么您可以使用 i/o 版本。
The string concatenation you're showing is perfectly valid. Although concatenation using stringstream is possible that is not the most useful feature of stringstreams, which is to be able to insert and extract POD and abstract data types.
您显示的字符串连接是完全有效的。尽管可以使用 stringstream 进行连接,但这并不是 stringstreams 最有用的特性,即能够插入和提取 POD 和抽象数据类型。
回答by Gerhard Wesp
std::ostringstream::str() creates a copy of the stream's content, which doubles memory usage in some situations. You can use std::stringstream and its rdbuf() function instead to avoid this.
std::ostringstream::str() 创建流内容的副本,这在某些情况下会加倍内存使用。您可以使用 std::stringstream 及其 rdbuf() 函数来避免这种情况。
More details here: how to write ostringstream directly to cout
更多细节在这里:如何将 ostringstream 直接写入 cout
回答by Assaf Lavie
Why open a file for read/write access if you only need to read from it, for example?
例如,如果您只需要读取文件,为什么要打开文件进行读/写访问?
What if multiple processes needed to read from the same file?
如果多个进程需要读取同一个文件怎么办?