istringstream、ostringstream 和 stringstream 之间有什么区别? / 为什么不在所有情况下都使用 stringstream?

发布于 2024-09-10 12:03:04 字数 312 浏览 3 评论 0原文

我什么时候会使用 std::istringstreamstd::ostringstreamstd::stringstream 以及为什么我不应该只使用 std::stringstream 在每个场景中(是否存在运行时性能问题?)。

最后,这有什么不好的地方(而不是使用流):

std::string stHehe("Hello ");

stHehe += "stackoverflow.com";
stHehe += "!";

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?).

Lastly, is there anything bad about this (instead of using a stream at all):

std::string stHehe("Hello ");

stHehe += "stackoverflow.com";
stHehe += "!";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

虚拟世界 2024-09-17 12:03:04

就我个人而言,我发现很少需要在同一字符串流中执行流式传输。

通常我想从字符串初始化一个流,然后解析它;或将内容流式传输到字符串流,然后提取结果并存储它。

如果您在同一个流之间进行流式传输,则必须非常小心流状态和流位置。

使用“just”istringstreamostringstream 可以更好地表达您的意图,并为您提供一些检查以防止愚蠢的错误,例如意外使用 << 与<代码>>>。

可能有一些性能改进,但我不会首先考虑这一点。

你写的没有什么问题。如果您发现它的性能不够好,那么您可以分析其他方法,否则坚持使用最清晰的方法。就我个人而言,我只想:

std::string stHehe( "Hello stackoverflow.com!" );

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 >>.

There might be 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!" );
活泼老夫 2024-09-17 12:03:04

stringstream 稍大一些,并且性能可能稍低——多重继承可能需要调整 vtable 指针。主要区别是(至少在理论上)更好地表达您的意图,并防止您意外地在您想要 << 的地方使用 >>(反之亦然) )。 OTOH,差异足够小,特别是对于快速的演示代码等,我很懒,只使用 stringstream 。我不太记得上次我打算使用 >> 时意外使用 << 是什么时候,所以对我来说,安全性似乎主要是理论上的(尤其是因为如果您确实犯了这样的错误,它几乎总是非常立即显而易见)。

仅仅使用字符串并没有什么问题,只要它能达到你想要的效果即可。如果您只是将字符串放在一起,那么很容易并且效果很好。如果您想格式化其他类型的数据,stringstream 将支持该功能,而字符串大多不会。

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 do make such a mistake, it'll almost always be really obvious almost immediately).

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.

我不吻晚风 2024-09-17 12:03:04

在大多数情况下,您不会发现自己需要在同一个字符串流上同时输入和输出,因此使用 std::ostringstreamstd::istringstream 明确地使您的意图清晰。它还可以防止您意外输入错误的运算符(<<>>)。

当您需要在同一个流上执行这两种操作时,您显然会使用通用版本。

性能问题在这里是您最不关心的问题,清晰度是主要优势。

最后,使用字符串附加没有任何问题,因为您必须构造纯字符串。您只是不能像在 perl 等语言中那样使用它来组合数字。

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 >>).

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.

痞味浪人 2024-09-17 12:03:04

istringstream 用于输入,ostringstream 用于输出。 stringstream是输入和输出。
您几乎可以在任何地方使用 stringstream。
但是,如果您将对象提供给另一个用户,并且它使用运算符 >>当你等待一个只写对象时,你不会高兴;-)

PS:
没什么不好,只是性能问题。

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 ;-)

PS:
nothing bad about it, just performance issues.

z祗昰~ 2024-09-17 12:03:04

std::ostringstream::str() 创建流内容的副本,这在某些情况下会使内存使用量增加一倍。您可以使用 std::stringstream 及其 rdbuf() 函数来避免这种情况。

更多详细信息请参见:如何将 ostringstream 直接写入 cout

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.

More details here: how to write ostringstream directly to cout

缘字诀 2024-09-17 12:03:04

回答你的第三个问题:不,这是完全合理的。使用流的优点是,您可以输入定义了运算符<<的任何类型的值,而只能将字符串(C++ 或 C)添加到 std::字符串。

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.

未央 2024-09-17 12:03:04

据推测,当仅插入或仅提取适合您的操作时,您可以使用“i”或“o”前缀版本之一来排除不需要的操作。

如果这不重要,那么您可以使用 i/o 版本。

您显示的字符串连接完全有效。尽管使用 stringstream 进行串联是可能的,但这并不是 stringstreams 最有用的功能,它能够插入和提取 POD 和抽象数据类型。

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.

If that is not important then you can use the i/o version.

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.

離殇 2024-09-17 12:03:04

例如,如果您只需要读取文件,为什么要打开文件进行读/写访问?

如果多个进程需要读取同一个文件怎么办?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文