What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

后端 未结 8 1725
春和景丽
春和景丽 2020-12-02 04:48

When would I use std::istringstream, std::ostringstream and std::stringstream and why shouldn\'t I just use std::stringstream

相关标签:
8条回答
  • 2020-12-02 05:08

    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.

    0 讨论(0)
  • 2020-12-02 05:08

    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?

    0 讨论(0)
  • 2020-12-02 05:13

    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

    0 讨论(0)
  • 2020-12-02 05:19

    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!" );
    
    0 讨论(0)
  • 2020-12-02 05:19

    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.

    0 讨论(0)
  • 2020-12-02 05:27

    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.

    0 讨论(0)
提交回复
热议问题