How can I convert int to std :: string in C ++ using MinGW?
Std :: to_string is known not to work. Even std :: itoa didn't work for me. What can I do to convert an int to a string? I don't care about performance, all I need is to work, not too slow.
Edit: I have the latest version of mingw 32 installed, std :: to_string still doesn't work. I installed it here: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/32-bit/threads-win32/sjlj/
source to share
It took me a few hours to solve this problem, and I ended up writing my own function for myself. This is probably not optimal and could be wrong as I am new to C ++ (this is literally the first useful function I wrote in C ++). However, it passed all the tests I wrote just fine, and performance shouldn't be an issue if the in-string conversion isn't 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;
}
source to share