itoa()的C ++标准替代,将int转换为基数10 char *

时间:2013-11-17 18:31:26

标签: c++ windows string int itoa

将整数转换为基数10 char *

std::itoa(ConCounter, ID, 10);

ConCounter是一个整数,ID是char *,10是基础

它说iota不是std的成员,没有std它没有声明。我知道这是一个非标准的功能,但我包含了它的所有库,它仍然没有看到它。

上述方法是什么?任何一个快速的衬垫? 我尝试了以下内容;

std::to_string //it's not declared for me when using mingw, it doesn't exist.
snprintf/sprintf //should work but it gives me the "invalid conversion from 'int' to 'char *'"    error
std::stoi //has same problem as iota

3 个答案:

答案 0 :(得分:6)

试试这个:

#include <sstream>

int i = // your number
std::ostringstream digit;
digit<<i;
std::string numberString(digit.str());

答案 1 :(得分:1)

我建议使用roybatty的答案,但我认为sprintf也应该有用。我想当你使用它时你忘记了格式字符串。它应该是:

char buf[16];
std::snprintf(buf, sizeof(buf), "%d", integer);

答案 2 :(得分:0)