如何将一个整数值传递给(const char*str)函数参数?

Kir*_*ran 0 c++

可能重复:
如何在C++中将数字转换为字符串,反之亦然
如何从int转换为char*?

我得到一个整数的用户输入,我需要将它们传递给一个参数 - 输出(char const*str); 这是一个Class构造函数.你能告诉我我该怎么办?谢谢

Mik*_*our 6

在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)