C++中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);
功能:
将数值转化为字符串。返回对应的字符串。
示例:
#include
#include
using namespace std;
int main ()
{
string pi = “pi is " + std::to_string(3.1415926);
string perfect = std::to_string(1+2+4+7+14) + " is a perfect number”;
cout << pi << ‘\n’;
cout << perfect << ‘\n’;
return 0;
}
【C++中to_string()函数用法讲解】结果:
pi is 3.141593
28 is a perfect number

    推荐阅读