繁体   English   中英

替代itoa()的C ++标准,用于将int转换为以10为基数的字符*

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

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

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

尝试这个:

#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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM