to_string()函数的作用为将数值转化为字符串。返回对应的字符串。
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 <iostream>
#include <cstring>
using namespace std;
int main(){
long long t = 4613654879654;
string s = to_string(t);
cout << t << endl;
return 0;
}
/*
结果为:
4613654879654
*/
#include <iostream>
#include <cstring>
using namespace std;
int main(){
double t = 3.141592;
string s = to_string(t);
cout << t << endl;
return 0;
}
/*
结果为:
3.14159
*/