0

将整数转换为以 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
4

3 回答 3

6

试试这个:

#include <sstream>

int i = // your number
std::ostringstream digit;
digit<<i;
std::string numberString(digit.str());
于 2013-11-17T18:34:48.740 回答
1

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

char buf[16];
std::snprintf(buf, sizeof(buf), "%d", integer);
于 2013-11-17T18:45:42.217 回答
0

还有“strtol”功能:http ://www.cplusplus.com/reference/cstdlib/strtol/

于 2013-11-17T18:37:24.913 回答