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

C++ standard alternative to itoa() to convert int to base 10 char*

本文关键字:转换 char int 用于 C++ 标准 替代品 itoa      更新时间:2023-10-16

将整数转换为基数为10的字符*

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

ConCounter是一个整数,ID是一个字符*,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

试试这个:

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

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

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

还有"strtol"函数:http://www.cplusplus.com/reference/cstdlib/strtol/