std :: to_string不起作用是一个已知问题。即使是std :: itoa也不适合我。如何将int转换为字符串?我不太关心性能,我所需要的只是工作而不是太慢。
编辑:我安装了最新的mingw 32,std :: to_string仍然无法正常工作。我是从这里安装的:http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/32-bit/threads-win32/sjlj/
答案 0 :(得分:4)
您是否考虑过使用字符串流?
#include <sstream>
std::string itos(int i){
std::stringstream ss;
ss<<i;
return ss.str();
}
答案 1 :(得分:0)
我花了几个小时来解决这个问题,最后我为此编写了自己的功能。它可能不是最优的,可能是有缺陷的,因为我是C ++的新手(这实际上是我在C ++中编写的第一个有用的函数)。但是,它完全通过了我写的所有测试,除非int-string转换是你瓶颈的重要部分,否则性能不应成为问题。
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;
}