0

It is a known issue that std::to_string does not work. Even std::itoa didn't work for me. What can I do to convert an int to a string? I don't care much about performance, all I need it is to work without being too slow.

Edit: I have the latest mingw 32 installed, std::to_string still does not work. I installed it from here: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/32-bit/threads-win32/sjlj/

Velizar Hristov
  • 662
  • 2
  • 10
  • 23
  • 2
    As I've mentioned [here](http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-so-g/12975602#12975602), recent versions of MinGW have `std::to_string`. So the answer is: update your compiler. Also I'm closing as a duplicate, since the linked questions provides several work arounds. – Rapptz Jul 25 '13 at 15:59
  • That answer was very misleading, as I had assumed that MinGW 64 works only for 64-bit Windows. Still trying to figure out how to install it. – Velizar Hristov Jul 25 '13 at 18:06
  • If for whatever reason you don't have a 64-bit OS, MinGW builds has a 32-bit version that you can use. – Rapptz Jul 25 '13 at 18:10
  • I am still trying to install it. The answer in the URL does not answer the question of "how do I convert an int to a C++ string on Windows. There are several options such as POSIX/win32, dwarf/sjlj and I have no idea what they mean. In order to be able to use this, I must spend more of my time to research them. – Velizar Hristov Jul 25 '13 at 18:29

2 Answers2

5

Have you considered using a stringstream?

#include <sstream>

std::string itos(int i){
    std::stringstream ss;
    ss<<i;
    return ss.str();
}
fatsmcgee
  • 314
  • 1
  • 4
0

It took me several hours to get over this issue, and I ended up writing my own function for that. It is probably not optimal and might be flawed, as I am new to C++ (this is literally the first useful function I wrote in C++). However, it passed perfectly all the tests I wrote, and the performance shouldn't be an issue unless int-string conversion is a significant part of your bottleneck.

string itos(int i)
{
    if (i == 0) { return "0"; }
    if (i < 0) { return "-" + itos(-i); }
    // Number of characters needed
    int size = int(log10(i)) + 1;
    char* buffer = (char*)malloc((size + 1) * sizeof(char));
    buffer[size] = NULL;
    for (int j = 0; j < size; j++)
        buffer[j] = (char) ( (int(i / pow(10, (size - j - 1))) % 10) + '0');
    string l(buffer);
    free(buffer);
    return l;
}
Velizar Hristov
  • 662
  • 2
  • 10
  • 23
  • 2
    I recommend avoiding malloc and just using `std::string` directly; see example: http://coliru.stacked-crooked.com/view?id=5ac5aaae1ec5c8826873ff36d4f121b3-90ec954ccadc610c9c71912ef5100471/. There are other possible improvements that could be done, but I think that one is important because it shows you how to avoid `malloc`. In that example also included a common alternative using stringstreams (see `itos2`). – R. Martinho Fernandes Jul 25 '13 at 16:06