181

我什么时候使用,std::istringstream为什么不应该只在每个场景中使用(是否存在任何运行时性能问题?)。std::ostringstreamstd::stringstreamstd::stringstream

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

std::string stHehe("Hello ");

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

8 回答 8

131

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!" );
于 2010-07-20T16:25:39.603 回答
25

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.

于 2010-07-20T16:27:11.370 回答
20

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

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

性能问题将是您最不关心的问题,清晰是主要优势。

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

于 2010-07-20T16:29:56.977 回答
12

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.

于 2010-07-20T16:26:55.873 回答
3

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.

于 2010-07-20T16:28:31.447 回答
3

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

更多细节在这里:如何将ostringstream直接写入cout

于 2019-05-12T13:54:19.143 回答
1

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.

于 2010-07-20T16:29:07.123 回答
0

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?

于 2010-07-20T16:27:43.777 回答