我得到一个整数的用户输入,我需要将它们传递给一个参数 - 输出(char const*str); 这是一个Class构造函数.你能告诉我我该怎么办?谢谢
在C++ 11中:
dodgy_function(std::to_string(value).c_str());
Run Code Online (Sandbox Code Playgroud)
在旧版语言中:
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
}
Run Code Online (Sandbox Code Playgroud)