如何将整数值传递给(const char*str)函数参数
How to Pass an integer value in to (const char *str) function argument?
可能重复:
如何在C++中将数字转换为字符串,反之亦然
如何从int转换为char*?
我得到了一个整数的用户输入,我需要将它们传递给一个参数-Output(char const*str);这是一个类构造函数。你能告诉我该怎么做吗?感谢
在C++11中:
dodgy_function(std::to_string(value).c_str());
在较旧的语言版本中:
std::ostringstream ss;
ss << value;
dodgy_function(ss.str().c_str());
// or
dodgy_function(boost::lexical_cast<std::string>(value).c_str());
// or in special circumstances
char buffer[i_hope_this_is_big_enough];
if (std::snprintf(buffer, sizeof buffer, "%d", value) < sizeof buffer) {
dodgy_function(buffer);
} else {
// The buffer was too small - deal with it
}