一、int转换成string
Ⅰ、to_string函数
c++11标准增加了全局函数std::to_string:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
Example:
1 // to_string example 2 #include <iostream> // std::cout 3 #include <string> // std::string, std::to_string 4 5 int main () 6 { 7 std::string pi = "pi is " + std::to_string(3.1415926); 8 std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number"; 9 std::cout << pi << \'\n\'; 10 std::cout << perfect << \'\n\'; 11 return 0; 12 } 13 Output 14 pi is 3.141593 15 28 is a perfect number