可能重复:
How to convert a number to string and vice versa in C++
how to convert from int to char*?
我正在获取整数的用户输入,我需要将它们传递给参数 - Output(char const * str);这是一个Class构造函数。你能告诉我我该怎么办?谢谢
答案 0 :(得分:6)
在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
}