c++ - 如何将整数转换为字符串

标签 c++ casting

<分区>

我想将整数转换为字符串。我这样试过,但没用

void foo()
{
    int y = 1;
    string x = static_cast<string>(y);

}

最佳答案

std::to_string函数应该这样做:

string x = std::to_string(y);

相反,它是std::stoi :

int z = std::stoi(y, nullptr, 10);

关于c++ - 如何将整数转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989521/

相关文章:

scala - scala中“Convert” Option [x]到x

c# - 为什么这个 C# 转换示例不起作用?

python - 如何在 python 中使用循环进行正确的转换?

error-handling - iOS Swift5如何检查对象的类型是否在[Class.Type]类型的数组中声明?

c++ - Soci 作为 linux/ubuntu 上的静态库?

c++ - 外部模板和不完整类型

python - 执行销毁令

c++ - C++访问冲突

c++ - 在 boost::singleton_pool 中创建对象

arrays - 如何将 const unsigned char 数组中的元素转换为 char