0

I this a good way to convert int to string?

    int a = 123456789;
    string str = static_cast<ostringstream*>(&(ostringstream()<<a))->str();
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
rondo
  • 63
  • 8
  • Possibly looking for http://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c?lq=1, but that cast could be to a reference and spare the ugly pointer syntax. – chris Mar 29 '15 at 18:54
  • Check http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c – skjcyber Mar 29 '15 at 18:55

1 Answers1

4

C++11 introduced std::to_string for this very purpose:

int a = 123456789;
std::string str = std::to_string(a);
fredoverflow
  • 256,549
  • 94
  • 388
  • 662