4

I've been using stringstream to convert Integer to String, but then I realized same operation can be done with ostringstream.

When I use .str() what is the difference between them? Also, is there more efficient way to convert integers to strings?

Sample code:

//using ostringstream

ostringstream s1;
int i=100;
s1<<i;
string str_i=s1.str();
cout<<str_i<<endl;

//using stringstream

stringstream s2;
int i2=100;
s2<<i2;
string str_i2=s2.str();
cout<<str_i2<<endl;
Brian
  • 14,610
  • 7
  • 35
  • 43
Malkavian
  • 372
  • 1
  • 5
  • 16
  • Already answered at http://stackoverflow.com/questions/3292107/whats-the-difference-between-istringstream-ostringstream-and-stringstream-w fnprintf does the same job faster but it depends on your code ofcourse. – cateof Jan 09 '12 at 21:01
  • 4
    C++11 has [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string) – Benjamin Lindley Jan 09 '12 at 21:09
  • @cateof Sorry, I didn't see that post. – Malkavian Jan 09 '12 at 21:17
  • @BenjaminLindley whoaaaa C++11 blows my mind again. I thought all we got was string -> int with `stoi` and friends, but I didn't know we have the other way too. – Seth Carnegie Jan 09 '12 at 21:19

1 Answers1

11

There is a third that you didn't mention, istringstream, which you can't use (well you could but it would be different, you can't << to an istringstream).

stringstream is both an ostringstream and an istringstream - you can << and >> both ways, in and out.

With ostringstream, you can only go in with <<, and you cannot go out with >>.

There isn't really a difference, you can use either way to convert strings to integers. If you want to do it the fastest way possible, I think boost::lexical_cast has that title, or you could use the itoa function which may be faster than stringstream, but you lose the advantages of C++ and the standard library if you use itoa (you have to use C-strings, etc).

Also, as Benjamin Lindley informed us, C++11 has the ultramagical std::to_string.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Thank you so much!, I'll definitely try the methods you mentioned. – Malkavian Jan 09 '12 at 21:14
  • I think `boost::lexical_cast` is great, but it's probably not the best in terms of run-time performance. It's just a wrapper around the `ostringstream` technique. See [The String Formatters of Manor Farm](http://www.gotw.ca/publications/mill19.htm). – Fred Larson Jan 09 '12 at 21:17
  • 1
    @FredLarson I thought boost did some specialisation for integers or something that made it faster. Maybe I just misread something somewhere. – Seth Carnegie Jan 09 '12 at 21:21
  • @SethCarnegie: Oh, perhaps. That would be a great idea. – Fred Larson Jan 09 '12 at 21:25
  • @FredLarson, I don't think that's necessarily true anymore. As of [Boost 1.48.0](http://www.boost.org/doc/libs/1_48_0/boost/lexical_cast.hpp), there is *way* more code in there than just a simple wrapper around a `stringstream`. – Michael Kristofik Jan 09 '12 at 21:26
  • [Boost.Spirit](http://www.boost.org/libs/spirit/) has the title of "fastest", but it comes with a not-insignificant compile-time cost. Boost.LexicalCast just wraps around `snprintf`. – ildjarn Jan 09 '12 at 21:27
  • Boost.LexicalCast's number to string conversions are okay, but their string to number conversions are pretty terrible. – Benjamin Lindley Jan 09 '12 at 21:44
  • std::string str(itoa(numberVar,tempChars,10)); – Catalyst Jun 09 '14 at 18:49