且构网

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

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

更新时间:2023-11-07 16:07:22

C ++ 11引入 std :: stoi (以及每个数字类型的变体)和 std :: to_string ,相应的的C atoi itoa ,但以 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);

因此是我能想到的最短的方式。

is therefore the shortest way I can think of.

注意:请参阅 [string.conversions] (n3242中的 21.5

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