且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 C++ 中将 int 转换为字符串的最简单方法

更新时间:2021-11-16 05:29:53

C++11 引入 std::stoi(以及每个数字类型的变体)和 std::to_string,C atoiitoa 的对应物,但用术语表示std::string.

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string.

#include <string> 

std::string s = std::to_string(42);

因此是我能想到的最短方法.您甚至可以省略命名类型,使用 auto 关键字:

is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword:

auto s = std::to_string(42);

注意:参见[string.conversions](n3242)

Note: see [string.conversions] (21.5 in n3242)